blob: 03672411f4e08a0035b7bbe7ae55d39950e29768 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Chris Lattner31a54742010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greeneca9b04b2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greene066ed6a2009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greenee52fd872009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner1eb0ad02009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene4a37a692009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner08c97082009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42ceb472009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner31a54742010-01-16 21:57:06 +000037#include "llvm/Target/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/Target/TargetData.h"
David Greeneca9b04b2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046#include <cerrno>
47using namespace llvm;
48
Evan Cheng42ceb472009-03-25 01:47:28 +000049static cl::opt<cl::boolOrDefault>
50AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
51 cl::init(cl::BOU_UNSET));
52
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000054AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattner621c44d2009-08-22 20:48:53 +000055 const MCAsmInfo *T, bool VDef)
Daniel Dunbarb10d2222009-07-01 01:48:54 +000056 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattnera5ef4d32009-08-22 21:43:10 +000057 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000058
59 OutContext(*new MCContext()),
Chris Lattnera835afd2009-09-14 03:02:37 +000060 // FIXME: Pass instprinter to streamer.
61 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000062
Devang Patel042945f2010-01-16 06:09:35 +000063 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner2424eac2009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000065 switch (AsmVerbose) {
66 case cl::BOU_UNSET: VerboseAsm = VDef; break;
67 case cl::BOU_TRUE: VerboseAsm = true; break;
68 case cl::BOU_FALSE: VerboseAsm = false; break;
69 }
70}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000072AsmPrinter::~AsmPrinter() {
73 for (gcp_iterator I = GCMetadataPrinters.begin(),
74 E = GCMetadataPrinters.end(); I != E; ++I)
75 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000079}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Chris Lattner75bdd292009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerd69d8ad2009-08-18 06:15:16 +000085/// getCurrentSection() - Return the current section we are emitting to.
86const MCSection *AsmPrinter::getCurrentSection() const {
87 return OutStreamer.getCurrentSection();
88}
89
90
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greene066ed6a2009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +000097}
98
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattner63ab9bb2010-01-17 18:22:35 +0000104 Mang = new Mangler(*MAI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000106 // Allow the target to emit any magic that it wants at the start of the file.
107 EmitStartOfAsmFile(M);
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000108
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000109 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000110 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilson6ed4b622009-09-30 21:26:13 +0000111 debug info. If we don't, this at least helps the user find where
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000112 a function came from. */
113 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
114 }
115
Bob Wilsonb5f835e2009-09-30 22:06:26 +0000116 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
117 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000118 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
119 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000120 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (!M.getModuleInlineAsm().empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000123 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 << M.getModuleInlineAsm()
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000125 << '\n' << MAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 << " End of file scope inline assembly\n";
127
Chris Lattnerc73e8eb2009-09-24 05:44:53 +0000128 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
129 if (MMI)
130 MMI->AnalyzeModule(M);
131 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta5461b522009-11-14 07:22:25 +0000132 if (DW)
Chris Lattnerc73e8eb2009-09-24 05:44:53 +0000133 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel1a454842009-06-19 21:54:26 +0000134
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 return false;
136}
137
Chris Lattnerd9609472010-01-19 04:39:15 +0000138/// EmitGlobalVariable - Emit the specified global variable to the .s file.
139void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
140 if (!GV->hasInitializer()) // External globals require no code.
141 return;
142
143 // Check to see if this is a special global used by LLVM, if so, emit it.
144 if (EmitSpecialLLVMGlobal(GV))
145 return;
Chris Lattner410c9472010-01-19 05:38:33 +0000146
147 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
148 printVisibility(GVSym, GV->getVisibility());
149
150 if (MAI->hasDotTypeDotSizeDirective()) {
151 O << "\t.type\t" << *GVSym;
152 if (MAI->getCommentString()[0] != '@')
153 O << ",@object\n";
154 else
155 O << ",%object\n";
156 }
Chris Lattnerd9609472010-01-19 04:39:15 +0000157
Chris Lattner410c9472010-01-19 05:38:33 +0000158 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
159
160 const TargetData *TD = TM.getTargetData();
161 unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType());
162 unsigned AlignLog = TD->getPreferredAlignmentLog(GV);
163
Chris Lattnerfdcdb2c2010-01-19 05:51:42 +0000164 // Handle common and BSS local symbols (.lcomm).
165 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner410c9472010-01-19 05:38:33 +0000166 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
167
Chris Lattner410c9472010-01-19 05:38:33 +0000168 if (VerboseAsm) {
169 O.PadToColumn(MAI->getCommentColumn());
170 O << MAI->getCommentString() << ' ';
171 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattnerecf1cdc2010-01-19 06:01:04 +0000172 O << '\n';
Chris Lattner410c9472010-01-19 05:38:33 +0000173 }
Chris Lattnerf26c7792010-01-19 06:25:51 +0000174
175 // Handle common symbols.
Chris Lattnerfdcdb2c2010-01-19 05:51:42 +0000176 if (GVKind.isCommon()) {
177 // .comm _foo, 42, 4
Chris Lattnerecf1cdc2010-01-19 06:01:04 +0000178 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattnerf26c7792010-01-19 06:25:51 +0000179 return;
Chris Lattnerfdcdb2c2010-01-19 05:51:42 +0000180 }
Chris Lattnerf26c7792010-01-19 06:25:51 +0000181
182 // Handle local BSS symbols.
183 if (MAI->hasMachoZeroFillDirective()) {
184 const MCSection *TheSection =
185 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
186 // .zerofill __DATA, __bss, _foo, 400, 5
187 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
188 return;
189 }
190
191 if (const char *LComm = MAI->getLCOMMDirective()) {
192 // .lcomm _foo, 42
193 O << LComm << *GVSym << ',' << Size;
194 O << '\n';
195 return;
196 }
197
198 // .local _foo
199 O << "\t.local\t" << *GVSym << '\n';
200 // .comm _foo, 42, 4
201 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner410c9472010-01-19 05:38:33 +0000202 return;
203 }
204
205 const MCSection *TheSection =
206 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
207
208 // Handle the zerofill directive on darwin, which is a special form of BSS
209 // emission.
210 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
211 // .globl _foo
212 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
213 // .zerofill __DATA, __common, _foo, 400, 5
214 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
215 return;
216 }
217
218 OutStreamer.SwitchSection(TheSection);
219
220 // TODO: Factor into an 'emit linkage' thing that is shared with function
221 // bodies.
222 switch (GV->getLinkage()) {
223 case GlobalValue::CommonLinkage:
224 case GlobalValue::LinkOnceAnyLinkage:
225 case GlobalValue::LinkOnceODRLinkage:
226 case GlobalValue::WeakAnyLinkage:
227 case GlobalValue::WeakODRLinkage:
228 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner8c2d16d2010-01-19 06:41:24 +0000229 if (MAI->getWeakDefDirective() != 0) {
Chris Lattner410c9472010-01-19 05:38:33 +0000230 // .globl _foo
231 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
232 // .weak_definition _foo
Chris Lattner8c2d16d2010-01-19 06:41:24 +0000233 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::WeakDefinition);
Chris Lattner410c9472010-01-19 05:38:33 +0000234 } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
235 // .globl _foo
236 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
237 // .linkonce same_size
238 O << LinkOnce;
Chris Lattner8c2d16d2010-01-19 06:41:24 +0000239 } else {
240 // .weak _foo
241 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Weak);
242 }
Chris Lattner410c9472010-01-19 05:38:33 +0000243 break;
244 case GlobalValue::DLLExportLinkage:
245 case GlobalValue::AppendingLinkage:
246 // FIXME: appending linkage variables should go into a section of
247 // their name or something. For now, just emit them as external.
248 case GlobalValue::ExternalLinkage:
249 // If external or appending, declare as a global symbol.
250 // .globl _foo
251 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
252 break;
253 case GlobalValue::PrivateLinkage:
254 case GlobalValue::InternalLinkage:
255 break;
256 default:
257 llvm_unreachable("Unknown linkage type!");
258 }
259
260 EmitAlignment(AlignLog, GV);
Chris Lattner410c9472010-01-19 05:38:33 +0000261 if (VerboseAsm) {
262 O.PadToColumn(MAI->getCommentColumn());
263 O << MAI->getCommentString() << ' ';
264 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattner8c2d16d2010-01-19 06:41:24 +0000265 O << '\n';
Chris Lattner410c9472010-01-19 05:38:33 +0000266 }
Chris Lattner8c2d16d2010-01-19 06:41:24 +0000267 OutStreamer.EmitLabel(GVSym);
Chris Lattner410c9472010-01-19 05:38:33 +0000268
269 EmitGlobalConstant(GV->getInitializer());
270
271 if (MAI->hasDotTypeDotSizeDirective())
272 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
Chris Lattnerd9609472010-01-19 04:39:15 +0000273}
274
275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000277 // Emit global variables.
278 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
279 I != E; ++I)
Chris Lattnerd9609472010-01-19 04:39:15 +0000280 EmitGlobalVariable(I);
Chris Lattnerae982212009-07-21 18:38:57 +0000281
Chris Lattnere1225cc2009-06-24 18:54:37 +0000282 // Emit final debug information.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000283 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattnere1225cc2009-06-24 18:54:37 +0000284 DW->EndModule();
285
Chris Lattnerdb191f02009-06-24 18:52:01 +0000286 // If the target wants to know about weak references, print them all.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000287 if (MAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000288 // FIXME: This is not lazy, it would be nice to only print weak references
289 // to stuff that is actually used. Note that doing so would require targets
290 // to notice uses in operands (due to constant exprs etc). This should
291 // happen with the MC stuff eventually.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292
Chris Lattnerdb191f02009-06-24 18:52:01 +0000293 // Print out module-level global variables here.
294 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
295 I != E; ++I) {
Chris Lattnercbfe4062010-01-16 18:17:26 +0000296 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattnerce409842010-01-17 21:43:43 +0000297 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000298 }
299
Chris Lattner0f98c332009-08-03 23:10:34 +0000300 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnercbfe4062010-01-16 18:17:26 +0000301 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattnerce409842010-01-17 21:43:43 +0000302 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000303 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304 }
305
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000306 if (MAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000307 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000309 I != E; ++I) {
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000310 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000311
312 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000313 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000314
Chris Lattnerce409842010-01-17 21:43:43 +0000315 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
316 O << "\t.globl\t" << *Name << '\n';
317 else if (I->hasWeakLinkage())
318 O << MAI->getWeakRefDirective() << *Name << '\n';
319 else
Chris Lattner1ff31942010-01-16 01:37:14 +0000320 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000321
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000322 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000323
Chris Lattnerce409842010-01-17 21:43:43 +0000324 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 }
326 }
327
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000328 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000329 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
330 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
331 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000332 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000333
Dan Gohmana65530a2008-05-05 00:28:39 +0000334 // If we don't have any trampolines, then we don't require stack memory
335 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000336 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000337 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000338 if (MAI->getNonexecutableStackDirective())
339 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000340
Chris Lattnerb6113072009-09-18 20:17:03 +0000341
342 // Allow the target to emit any magic that it wants at the end of the file,
343 // after everything else has gone out.
344 EmitEndOfAsmFile(M);
345
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000347 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000348
349 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 return false;
351}
352
353void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnerac5a6ad2010-01-16 01:24:10 +0000354 // Get the function symbol.
Chris Lattner8d656d92010-01-15 23:55:16 +0000355 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000356 IncrementFunctionNumber();
David Greenee52fd872009-08-10 16:38:07 +0000357
Chris Lattner57c76b52009-09-16 00:35:39 +0000358 if (VerboseAsm)
David Greenee52fd872009-08-10 16:38:07 +0000359 LI = &getAnalysis<MachineLoopInfo>();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360}
361
Evan Cheng68c18682009-03-13 07:51:59 +0000362namespace {
363 // SectionCPs - Keep track the alignment, constpool entries per Section.
364 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000365 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000366 unsigned Alignment;
367 SmallVector<unsigned, 4> CPEs;
Douglas Gregor8cb41382009-12-19 07:05:23 +0000368 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng68c18682009-03-13 07:51:59 +0000369 };
370}
371
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372/// EmitConstantPool - Print to the current output stream assembly
373/// representations of the constants in the constant pool MCP. This is
374/// used to print out constants which have been "spilled to memory" by
375/// the code generator.
376///
377void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
378 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
379 if (CP.empty()) return;
380
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000381 // Calculate sections for constant pool entries. We collect entries to go into
382 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000383 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000385 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000386 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000387
388 SectionKind Kind;
389 switch (CPE.getRelocationInfo()) {
390 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000391 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000392 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000393 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000394 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000395 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000396 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000397 case 4: Kind = SectionKind::getMergeableConst4(); break;
398 case 8: Kind = SectionKind::getMergeableConst8(); break;
399 case 16: Kind = SectionKind::getMergeableConst16();break;
400 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000401 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000402 }
403
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000404 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000405
Evan Cheng68c18682009-03-13 07:51:59 +0000406 // The number of sections are small, just do a linear search from the
407 // last section to the first.
408 bool Found = false;
409 unsigned SecIdx = CPSections.size();
410 while (SecIdx != 0) {
411 if (CPSections[--SecIdx].S == S) {
412 Found = true;
413 break;
414 }
415 }
416 if (!Found) {
417 SecIdx = CPSections.size();
418 CPSections.push_back(SectionCPs(S, Align));
419 }
420
421 if (Align > CPSections[SecIdx].Alignment)
422 CPSections[SecIdx].Alignment = Align;
423 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 }
425
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000426 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000427 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner73266f92009-08-19 05:49:37 +0000428 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng68c18682009-03-13 07:51:59 +0000429 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430
Evan Cheng68c18682009-03-13 07:51:59 +0000431 unsigned Offset = 0;
432 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
433 unsigned CPI = CPSections[i].CPEs[j];
434 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000435
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000436 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000437 unsigned AlignMask = CPE.getAlignment() - 1;
438 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Chris Lattnera71dc602010-01-19 19:46:13 +0000439 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
Evan Cheng68c18682009-03-13 07:51:59 +0000440
441 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000442 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000443
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000444 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohman87581eb2009-08-12 18:32:22 +0000445 << CPI << ':';
Evan Cheng68c18682009-03-13 07:51:59 +0000446 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000447 O.PadToColumn(MAI->getCommentColumn());
448 O << MAI->getCommentString() << " constant ";
Dan Gohman2aa282f2009-08-13 01:36:44 +0000449 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000450 }
Evan Cheng68c18682009-03-13 07:51:59 +0000451 O << '\n';
452 if (CPE.isMachineConstantPoolEntry())
453 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
454 else
455 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 }
457 }
458}
459
460/// EmitJumpTableInfo - Print assembly representations of the jump tables used
461/// by the current function to the current output stream.
462///
463void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
464 MachineFunction &MF) {
465 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
466 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000467
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000468 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
469
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 // Pick the directive to use to print the jump table entries, and switch to
471 // the appropriate section.
472 TargetLowering *LoweringInfo = TM.getTargetLowering();
473
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000474 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000475 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000476 if (F->isWeakForLinker() ||
477 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 // In PIC mode, we need to emit the jump table to the same section as the
479 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000480 // We should also do if the section name is NULL or function is declared in
481 // discardable section.
Chris Lattner73266f92009-08-19 05:49:37 +0000482 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
483 TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000485 // Otherwise, drop it in the readonly section.
486 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000487 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner73266f92009-08-19 05:49:37 +0000488 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000489 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000490 }
491
492 EmitAlignment(Log2_32(MJTI->getAlignment()));
493
494 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
495 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
496
497 // If this jump table was deleted, ignore it.
498 if (JTBBs.empty()) continue;
499
500 // For PIC codegen, if possible we want to use the SetDirective to reduce
501 // the number of relocations the assembler will generate for the jump table.
502 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000503 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000504 if (MAI->getSetDirective() && IsPic)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000506 if (EmittedSets.insert(JTBBs[ii]))
507 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508
Chris Lattner149495f2009-09-13 19:02:16 +0000509 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 // before each jump table. The first label is never referenced, but tells
511 // the assembler and linker the extents of the jump table object. The
512 // second label is actually referenced by the code.
Chris Lattner149495f2009-09-13 19:02:16 +0000513 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
514 O << MAI->getLinkerPrivateGlobalPrefix()
515 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengccca6f72009-06-18 20:37:15 +0000516 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000518 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +0000519 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520
521 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000522 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 O << '\n';
524 }
525 }
526}
527
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000528void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
529 const MachineBasicBlock *MBB,
530 unsigned uid) const {
Chris Lattner3fb451e2009-08-11 20:29:57 +0000531 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000532
533 // Use JumpTableDirective otherwise honor the entry size from the jump table
534 // info.
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000535 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000536 bool HadJTEntryDirective = JTEntryDirective != NULL;
537 if (!HadJTEntryDirective) {
538 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000539 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000540 }
541
542 O << JTEntryDirective << ' ';
543
544 // If we have emitted set directives for the jump table entries, print
545 // them rather than the entries themselves. If we're emitting PIC, then
546 // emit the table entries as differences between two text section labels.
547 // If we're emitting non-PIC code, then emit the entries as direct
548 // references to the target basic blocks.
Chris Lattner3fb451e2009-08-11 20:29:57 +0000549 if (!isPIC) {
Chris Lattnerce409842010-01-17 21:43:43 +0000550 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000551 } else if (MAI->getSetDirective()) {
552 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattner3fb451e2009-08-11 20:29:57 +0000553 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000554 } else {
Chris Lattnerce409842010-01-17 21:43:43 +0000555 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner3fb451e2009-08-11 20:29:57 +0000556 // If the arch uses custom Jump Table directives, don't calc relative to
557 // JT
558 if (!HadJTEntryDirective)
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000559 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattner3fb451e2009-08-11 20:29:57 +0000560 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000561 }
562}
563
564
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
566/// special global used by LLVM. If so, emit it and return true, otherwise
567/// do nothing and return false.
568bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000569 if (GV->getName() == "llvm.used") {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000570 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000571 EmitLLVMUsedList(GV->getInitializer());
572 return true;
573 }
574
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000575 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000576 if (GV->getSection() == "llvm.metadata" ||
577 GV->hasAvailableExternallyLinkage())
578 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579
580 if (!GV->hasAppendingLinkage()) return false;
581
582 assert(GV->hasInitializer() && "Not a special LLVM global!");
583
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000584 const TargetData *TD = TM.getTargetData();
585 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000586 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000587 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000588 EmitAlignment(Align, 0);
589 EmitXXStructorList(GV->getInitializer());
Chris Lattner2f2fe052010-01-19 04:34:02 +0000590
591 if (TM.getRelocationModel() == Reloc::Static &&
592 MAI->hasStaticCtorDtorReferenceInStaticMode())
593 O << ".reference .constructors_used\n";
Chris Lattner1e0e4892009-03-09 08:18:48 +0000594 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595 }
596
Chris Lattner0c433e92009-03-09 05:52:15 +0000597 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner73266f92009-08-19 05:49:37 +0000598 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000599 EmitAlignment(Align, 0);
600 EmitXXStructorList(GV->getInitializer());
Chris Lattner2f2fe052010-01-19 04:34:02 +0000601
602 if (TM.getRelocationModel() == Reloc::Static &&
603 MAI->hasStaticCtorDtorReferenceInStaticMode())
604 O << ".reference .destructors_used\n";
Chris Lattner1e0e4892009-03-09 08:18:48 +0000605 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606 }
607
608 return false;
609}
610
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000611/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000612/// global in the specified llvm.used list for which emitUsedDirectiveFor
613/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000615 const char *Directive = MAI->getUsedDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000616
Dan Gohman9e1657f2009-06-14 23:30:43 +0000617 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000618 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
619 if (InitList == 0) return;
620
621 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000622 const GlobalValue *GV =
623 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000624 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000625 O << Directive;
626 EmitConstantValueOnly(InitList->getOperand(i));
627 O << '\n';
628 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000629 }
630}
631
632/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
633/// function pointers, ignoring the init priority.
634void AsmPrinter::EmitXXStructorList(Constant *List) {
635 // Should be an array of '{ int, void ()* }' structs. The first value is the
636 // init priority, which we ignore.
637 if (!isa<ConstantArray>(List)) return;
638 ConstantArray *InitList = cast<ConstantArray>(List);
639 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
640 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
641 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
642
643 if (CS->getOperand(1)->isNullValue())
644 return; // Found a null terminator, exit printing.
645 // Emit the function pointer.
646 EmitGlobalConstant(CS->getOperand(1));
647 }
648}
649
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000650
651//===----------------------------------------------------------------------===//
652/// LEB 128 number encoding.
653
Bob Wilsoncda028f2009-11-06 22:38:38 +0000654/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000655/// representing an unsigned leb128 value.
656void AsmPrinter::PrintULEB128(unsigned Value) const {
657 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000658 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000659 Value >>= 7;
660 if (Value) Byte |= 0x80;
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000661 PrintHex(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000662 if (Value) O << ", ";
663 } while (Value);
664}
665
Bob Wilsoncda028f2009-11-06 22:38:38 +0000666/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000667/// representing a signed leb128 value.
668void AsmPrinter::PrintSLEB128(int Value) const {
669 int Sign = Value >> (8 * sizeof(Value) - 1);
670 bool IsMore;
aslc200b112008-08-16 12:57:46 +0000671
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000673 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000674 Value >>= 7;
675 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
676 if (IsMore) Byte |= 0x80;
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000677 PrintHex(Byte);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000678 if (IsMore) O << ", ";
679 } while (IsMore);
680}
681
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000682//===--------------------------------------------------------------------===//
683// Emission and print routines
684//
685
Bob Wilsoncda028f2009-11-06 22:38:38 +0000686/// PrintHex - Print a value as a hexadecimal value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000687///
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000688void AsmPrinter::PrintHex(uint64_t Value) const {
689 O << "0x";
690 O.write_hex(Value);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691}
692
693/// EOL - Print a newline character to asm stream. If a comment is present
694/// then it will be printed first. Comments should not contain '\n'.
695void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000696 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000698
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000699void AsmPrinter::EOL(const Twine &Comment) const {
700 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000701 O.PadToColumn(MAI->getCommentColumn());
702 O << MAI->getCommentString()
Owen Anderson367bfbb2008-07-01 21:16:27 +0000703 << ' '
704 << Comment;
705 }
706 O << '\n';
707}
708
Bill Wendling946b5212009-08-29 12:17:53 +0000709static const char *DecodeDWARFEncoding(unsigned Encoding) {
710 switch (Encoding) {
711 case dwarf::DW_EH_PE_absptr:
712 return "absptr";
713 case dwarf::DW_EH_PE_omit:
714 return "omit";
715 case dwarf::DW_EH_PE_pcrel:
716 return "pcrel";
Bill Wendlingbe23fd42009-09-09 21:26:19 +0000717 case dwarf::DW_EH_PE_udata4:
718 return "udata4";
719 case dwarf::DW_EH_PE_udata8:
720 return "udata8";
721 case dwarf::DW_EH_PE_sdata4:
722 return "sdata4";
723 case dwarf::DW_EH_PE_sdata8:
724 return "sdata8";
Bill Wendling946b5212009-08-29 12:17:53 +0000725 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
726 return "pcrel udata4";
727 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
728 return "pcrel sdata4";
729 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
730 return "pcrel udata8";
731 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
732 return "pcrel sdata8";
733 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
734 return "indirect pcrel udata4";
735 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
736 return "indirect pcrel sdata4";
737 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
738 return "indirect pcrel udata8";
739 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
740 return "indirect pcrel sdata8";
741 }
742
743 return 0;
744}
745
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000746void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
747 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendling946b5212009-08-29 12:17:53 +0000748 O.PadToColumn(MAI->getCommentColumn());
749 O << MAI->getCommentString()
750 << ' '
751 << Comment;
752
753 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
754 O << " (" << EncStr << ')';
755 }
756 O << '\n';
757}
758
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
760/// unsigned leb128 value.
761void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000762 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000763 O << "\t.uleb128\t"
764 << Value;
765 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000766 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000767 PrintULEB128(Value);
768 }
769}
770
771/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
772/// signed leb128 value.
773void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000774 if (MAI->hasLEB128()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775 O << "\t.sleb128\t"
776 << Value;
777 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000778 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000779 PrintSLEB128(Value);
780 }
781}
782
783/// EmitInt8 - Emit a byte directive and value.
784///
785void AsmPrinter::EmitInt8(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000786 O << MAI->getData8bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 PrintHex(Value & 0xFF);
788}
789
790/// EmitInt16 - Emit a short directive and value.
791///
792void AsmPrinter::EmitInt16(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000793 O << MAI->getData16bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000794 PrintHex(Value & 0xFFFF);
795}
796
797/// EmitInt32 - Emit a long directive and value.
798///
799void AsmPrinter::EmitInt32(int Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000800 O << MAI->getData32bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000801 PrintHex(Value);
802}
803
804/// EmitInt64 - Emit a long long directive and value.
805///
806void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000807 if (MAI->getData64bitsDirective()) {
808 O << MAI->getData64bitsDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000809 PrintHex(Value);
810 } else {
811 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000812 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000813 EmitInt32(unsigned(Value));
814 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000815 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 EmitInt32(unsigned(Value >> 32));
817 }
818 }
819}
820
821/// toOctal - Convert the low order bits of X into an octal digit.
822///
823static inline char toOctal(int X) {
824 return (X&7)+'0';
825}
826
827/// printStringChar - Print a char, escaped if necessary.
828///
David Greene302008d2009-07-14 20:18:05 +0000829static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 if (C == '"') {
831 O << "\\\"";
832 } else if (C == '\\') {
833 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000834 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000835 O << C;
836 } else {
837 switch(C) {
838 case '\b': O << "\\b"; break;
839 case '\f': O << "\\f"; break;
840 case '\n': O << "\\n"; break;
841 case '\r': O << "\\r"; break;
842 case '\t': O << "\\t"; break;
843 default:
844 O << '\\';
845 O << toOctal(C >> 6);
846 O << toOctal(C >> 3);
847 O << toOctal(C >> 0);
848 break;
849 }
850 }
851}
852
853/// EmitString - Emit a string with quotes and a null terminator.
854/// Special characters are emitted properly.
855/// \literal (Eg. '\t') \endliteral
Devang Patelc10337d2009-11-24 19:42:17 +0000856void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohmana5d39352009-11-13 21:55:31 +0000857 EmitString(String.data(), String.size());
Bill Wendling3f94b412009-04-09 23:51:31 +0000858}
859
860void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000861 const char* AscizDirective = MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000862 if (AscizDirective)
863 O << AscizDirective;
864 else
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000865 O << MAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000866 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000867 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000868 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000869 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000870 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 else
872 O << "\\0\"";
873}
874
875
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000876/// EmitFile - Emit a .file directive.
Benjamin Kramer0ad2db92010-01-17 07:46:39 +0000877void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000878 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000879 for (unsigned i = 0, N = Name.size(); i < N; ++i)
880 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000881 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000882}
883
884
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000885//===----------------------------------------------------------------------===//
886
887// EmitAlignment - Emit an alignment directive to the specified power of
888// two boundary. For example, if you pass in 3 here, you will get an 8
889// byte alignment. If a global value is specified, and if that global has
890// an explicit alignment requested, it will unconditionally override the
891// alignment request. However, if ForcedAlignBits is specified, this value
892// has final say: the ultimate alignment will be the max of ForcedAlignBits
893// and the alignment computed with NumBits and the global.
894//
895// The algorithm is:
896// Align = NumBits;
897// if (GV && GV->hasalignment) Align = GV->getalignment();
898// Align = std::max(Align, ForcedAlignBits);
899//
900void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000901 unsigned ForcedAlignBits,
902 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 if (GV && GV->getAlignment())
904 NumBits = Log2_32(GV->getAlignment());
905 NumBits = std::max(NumBits, ForcedAlignBits);
906
907 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner4891d142009-08-19 06:12:02 +0000908
909 unsigned FillValue = 0;
Chris Lattnerd69d8ad2009-08-18 06:15:16 +0000910 if (getCurrentSection()->getKind().isText())
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000911 FillValue = MAI->getTextAlignFillValue();
Chris Lattner4891d142009-08-19 06:12:02 +0000912
913 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000914}
David Greenecdc2de32009-08-05 21:00:52 +0000915
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916// Print out the specified constant, without a storage class. Only the
917// constants valid in constant expressions can occur here.
918void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattner458c8772010-01-13 04:29:19 +0000919 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000920 O << '0';
Chris Lattner458c8772010-01-13 04:29:19 +0000921 return;
922 }
923
924 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000925 O << CI->getZExtValue();
Chris Lattner458c8772010-01-13 04:29:19 +0000926 return;
927 }
928
929 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 // This is a constant address for a global variable or function. Use the
Chris Lattner36d03292009-09-15 23:11:32 +0000931 // name of the variable or function as the address value.
Chris Lattnerce409842010-01-17 21:43:43 +0000932 O << *GetGlobalValueSymbol(GV);
Chris Lattner458c8772010-01-13 04:29:19 +0000933 return;
934 }
935
936 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattnerce409842010-01-17 21:43:43 +0000937 O << *GetBlockAddressSymbol(BA);
Chris Lattner458c8772010-01-13 04:29:19 +0000938 return;
939 }
940
941 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
942 if (CE == 0) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000943 llvm_unreachable("Unknown constant value!");
Chris Lattner458c8772010-01-13 04:29:19 +0000944 O << '0';
945 return;
946 }
947
948 switch (CE->getOpcode()) {
949 case Instruction::ZExt:
950 case Instruction::SExt:
951 case Instruction::FPTrunc:
952 case Instruction::FPExt:
953 case Instruction::UIToFP:
954 case Instruction::SIToFP:
955 case Instruction::FPToUI:
956 case Instruction::FPToSI:
957 default:
958 llvm_unreachable("FIXME: Don't support this constant cast expr");
959 case Instruction::GetElementPtr: {
960 // generate a symbolic expression for the byte address
961 const TargetData *TD = TM.getTargetData();
962 const Constant *ptrVal = CE->getOperand(0);
963 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
964 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
965 idxVec.size());
966 if (Offset == 0)
967 return EmitConstantValueOnly(ptrVal);
968
969 // Truncate/sext the offset to the pointer size.
970 if (TD->getPointerSizeInBits() != 64) {
971 int SExtAmount = 64-TD->getPointerSizeInBits();
972 Offset = (Offset << SExtAmount) >> SExtAmount;
973 }
974
975 if (Offset)
976 O << '(';
977 EmitConstantValueOnly(ptrVal);
978 if (Offset > 0)
979 O << ") + " << Offset;
980 else
981 O << ") - " << -Offset;
982 return;
983 }
984 case Instruction::BitCast:
985 return EmitConstantValueOnly(CE->getOperand(0));
986
987 case Instruction::IntToPtr: {
988 // Handle casts to pointers by changing them into casts to the appropriate
989 // integer type. This promotes constant folding and simplifies this code.
990 const TargetData *TD = TM.getTargetData();
991 Constant *Op = CE->getOperand(0);
992 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
993 false/*ZExt*/);
994 return EmitConstantValueOnly(Op);
995 }
996
997 case Instruction::PtrToInt: {
998 // Support only foldable casts to/from pointers that can be eliminated by
999 // changing the pointer to the appropriately sized integer type.
1000 Constant *Op = CE->getOperand(0);
1001 const Type *Ty = CE->getType();
1002 const TargetData *TD = TM.getTargetData();
1003
1004 // We can emit the pointer value into this slot if the slot is an
1005 // integer slot greater or equal to the size of the pointer.
1006 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
1007 return EmitConstantValueOnly(Op);
1008
1009 O << "((";
1010 EmitConstantValueOnly(Op);
1011 APInt ptrMask =
1012 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
1013
1014 SmallString<40> S;
1015 ptrMask.toStringUnsigned(S);
1016 O << ") & " << S.str() << ')';
1017 return;
1018 }
1019
1020 case Instruction::Trunc:
1021 // We emit the value and depend on the assembler to truncate the generated
1022 // expression properly. This is important for differences between
1023 // blockaddress labels. Since the two labels are in the same function, it
1024 // is reasonable to treat their delta as a 32-bit value.
1025 return EmitConstantValueOnly(CE->getOperand(0));
1026
1027 case Instruction::Add:
1028 case Instruction::Sub:
1029 case Instruction::And:
1030 case Instruction::Or:
1031 case Instruction::Xor:
1032 O << '(';
1033 EmitConstantValueOnly(CE->getOperand(0));
1034 O << ')';
1035 switch (CE->getOpcode()) {
1036 case Instruction::Add:
1037 O << " + ";
1038 break;
1039 case Instruction::Sub:
1040 O << " - ";
1041 break;
1042 case Instruction::And:
1043 O << " & ";
1044 break;
1045 case Instruction::Or:
1046 O << " | ";
1047 break;
1048 case Instruction::Xor:
1049 O << " ^ ";
1050 break;
1051 default:
1052 break;
1053 }
1054 O << '(';
1055 EmitConstantValueOnly(CE->getOperand(1));
1056 O << ')';
1057 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001058 }
1059}
1060
Chris Lattnera71dc602010-01-19 19:46:13 +00001061/// EmitZeros - Emit a block of zeros.
1062///
1063void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
1064 OutStreamer.EmitFill(NumZeros, 0, AddrSpace);
1065}
1066
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001067/// printAsCString - Print the specified array as a C compatible string, only if
1068/// the predicate isString is true.
1069///
David Greene302008d2009-07-14 20:18:05 +00001070static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001071 unsigned LastElt) {
1072 assert(CVA->isString() && "Array is not string compatible!");
1073
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001074 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001075 for (unsigned i = 0; i != LastElt; ++i) {
1076 unsigned char C =
1077 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
1078 printStringChar(O, C);
1079 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001080 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001081}
1082
1083/// EmitString - Emit a zero-byte-terminated string constant.
1084///
1085void AsmPrinter::EmitString(const ConstantArray *CVA) const {
1086 unsigned NumElts = CVA->getNumOperands();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001087 if (MAI->getAscizDirective() && NumElts &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001088 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001089 O << MAI->getAscizDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001090 printAsCString(O, CVA, NumElts-1);
1091 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001092 O << MAI->getAsciiDirective();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001093 printAsCString(O, CVA, NumElts);
1094 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001095 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001096}
1097
Chris Lattnere72333d2010-01-19 19:10:44 +00001098static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
1099 AsmPrinter &AP) {
1100 if (AddrSpace == 0 && CA->isString()) {
1101 AP.EmitString(CA);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001102 } else { // Not a string. Print the values in successive locations
Chris Lattnere72333d2010-01-19 19:10:44 +00001103 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1104 AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001105 }
1106}
1107
Chris Lattnere72333d2010-01-19 19:10:44 +00001108static void EmitGlobalConstantVector(const ConstantVector *CV,
1109 unsigned AddrSpace, AsmPrinter &AP) {
1110 const VectorType *VTy = CV->getType();
1111 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
1112 AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001113}
1114
Chris Lattnere72333d2010-01-19 19:10:44 +00001115static void EmitGlobalConstantStruct(const ConstantStruct *CS,
1116 unsigned AddrSpace, AsmPrinter &AP) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001117 // Print the fields in successive locations. Pad to align if needed!
Chris Lattnere72333d2010-01-19 19:10:44 +00001118 const TargetData *TD = AP.TM.getTargetData();
1119 unsigned Size = TD->getTypeAllocSize(CS->getType());
1120 const StructLayout *cvsLayout = TD->getStructLayout(CS->getType());
1121 uint64_t SizeSoFar = 0;
1122 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
1123 const Constant *field = CS->getOperand(i);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001124
1125 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001126 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +00001127 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1128 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattnere72333d2010-01-19 19:10:44 +00001129 SizeSoFar += fieldSize + padSize;
Dan Gohmane78b0c72008-12-22 21:14:27 +00001130
1131 // Now print the actual field value.
Chris Lattnere72333d2010-01-19 19:10:44 +00001132 AP.EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001133
1134 // Insert padding - this may include padding to increase the size of the
1135 // current field up to the ABI size (if the struct is not packed) as well
1136 // as padding to ensure that the next field starts at the right offset.
Chris Lattnere72333d2010-01-19 19:10:44 +00001137 AP.EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001138 }
Chris Lattnere72333d2010-01-19 19:10:44 +00001139 assert(SizeSoFar == cvsLayout->getSizeInBytes() &&
Dan Gohmane78b0c72008-12-22 21:14:27 +00001140 "Layout of constant struct may be incorrect!");
1141}
1142
Chris Lattnere72333d2010-01-19 19:10:44 +00001143void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001144 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001145 // FP Constants are printed as integer constants to avoid losing
1146 // precision...
Chris Lattnere72333d2010-01-19 19:10:44 +00001147 const TargetData &TD = *TM.getTargetData();
Chris Lattner82cdc062009-10-05 05:54:46 +00001148 if (CFP->getType()->isDoubleTy()) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001149 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1150 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001151 if (MAI->getData64bitsDirective(AddrSpace)) {
1152 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohman87581eb2009-08-12 18:32:22 +00001153 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001154 O.PadToColumn(MAI->getCommentColumn());
1155 O << MAI->getCommentString() << " double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001156 }
Evan Cheng11db8142009-03-24 00:17:40 +00001157 O << '\n';
Chris Lattnere72333d2010-01-19 19:10:44 +00001158 } else if (TD.isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001159 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001160 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001161 O.PadToColumn(MAI->getCommentColumn());
1162 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001163 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001164 }
Evan Cheng11db8142009-03-24 00:17:40 +00001165 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001166 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001167 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001168 O.PadToColumn(MAI->getCommentColumn());
1169 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001170 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001171 }
Evan Cheng11db8142009-03-24 00:17:40 +00001172 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001173 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001174 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohman87581eb2009-08-12 18:32:22 +00001175 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001176 O.PadToColumn(MAI->getCommentColumn());
1177 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001178 << " least significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001179 }
Evan Cheng11db8142009-03-24 00:17:40 +00001180 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001181 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001182 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001183 O.PadToColumn(MAI->getCommentColumn());
1184 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001185 << " most significant word of double " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001186 }
Evan Cheng11db8142009-03-24 00:17:40 +00001187 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001188 }
1189 return;
Chris Lattner82cdc062009-10-05 05:54:46 +00001190 }
1191
1192 if (CFP->getType()->isFloatTy()) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001193 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001194 O << MAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001195 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohman87581eb2009-08-12 18:32:22 +00001196 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001197 O.PadToColumn(MAI->getCommentColumn());
1198 O << MAI->getCommentString() << " float " << Val;
Dan Gohman87581eb2009-08-12 18:32:22 +00001199 }
Evan Cheng11db8142009-03-24 00:17:40 +00001200 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001201 return;
Chris Lattner82cdc062009-10-05 05:54:46 +00001202 }
1203
1204 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001205 // all long double variants are printed as hex
1206 // api needed to prevent premature destruction
1207 APInt api = CFP->getValueAPF().bitcastToAPInt();
1208 const uint64_t *p = api.getRawData();
1209 // Convert to double so we can print the approximate val as a comment.
1210 APFloat DoubleVal = CFP->getValueAPF();
1211 bool ignored;
1212 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1213 &ignored);
Chris Lattnere72333d2010-01-19 19:10:44 +00001214 if (TD.isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001215 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001216 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001217 O.PadToColumn(MAI->getCommentColumn());
1218 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001219 << " most significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001220 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001221 }
Evan Cheng11db8142009-03-24 00:17:40 +00001222 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001223 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001224 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001225 O.PadToColumn(MAI->getCommentColumn());
1226 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001227 }
Evan Cheng11db8142009-03-24 00:17:40 +00001228 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001229 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001230 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001231 O.PadToColumn(MAI->getCommentColumn());
1232 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001233 }
Evan Cheng11db8142009-03-24 00:17:40 +00001234 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001235 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001236 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001237 O.PadToColumn(MAI->getCommentColumn());
1238 O << MAI->getCommentString() << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001239 }
Evan Cheng11db8142009-03-24 00:17:40 +00001240 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001241 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001242 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001243 O.PadToColumn(MAI->getCommentColumn());
1244 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001245 << " least significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001246 }
Evan Cheng11db8142009-03-24 00:17:40 +00001247 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001248 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001249 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001250 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001251 O.PadToColumn(MAI->getCommentColumn());
1252 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001253 << " least significant halfword of x86_fp80 ~"
Evan Cheng11db8142009-03-24 00:17:40 +00001254 << DoubleVal.convertToDouble();
Dan Gohman87581eb2009-08-12 18:32:22 +00001255 }
Evan Cheng11db8142009-03-24 00:17:40 +00001256 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001257 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohman87581eb2009-08-12 18:32:22 +00001258 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001259 O.PadToColumn(MAI->getCommentColumn());
1260 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001261 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001262 }
Evan Cheng11db8142009-03-24 00:17:40 +00001263 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001264 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001265 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001266 O.PadToColumn(MAI->getCommentColumn());
1267 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001268 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001269 }
Evan Cheng11db8142009-03-24 00:17:40 +00001270 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001271 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohman87581eb2009-08-12 18:32:22 +00001272 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001273 O.PadToColumn(MAI->getCommentColumn());
1274 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001275 << " next halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001276 }
Evan Cheng11db8142009-03-24 00:17:40 +00001277 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001278 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001279 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001280 O.PadToColumn(MAI->getCommentColumn());
1281 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001282 << " most significant halfword";
Dan Gohman87581eb2009-08-12 18:32:22 +00001283 }
Evan Cheng11db8142009-03-24 00:17:40 +00001284 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001285 }
Chris Lattnere72333d2010-01-19 19:10:44 +00001286 LLVMContext &Context = CFP->getContext();
1287 EmitZeros(TD.getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1288 TD.getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001289 return;
Chris Lattner82cdc062009-10-05 05:54:46 +00001290 }
1291
1292 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001293 // all long double variants are printed as hex
1294 // api needed to prevent premature destruction
1295 APInt api = CFP->getValueAPF().bitcastToAPInt();
1296 const uint64_t *p = api.getRawData();
Chris Lattnere72333d2010-01-19 19:10:44 +00001297 if (TD.isBigEndian()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001298 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001299 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001300 O.PadToColumn(MAI->getCommentColumn());
1301 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001302 << " most significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001303 }
Evan Cheng11db8142009-03-24 00:17:40 +00001304 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001305 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001306 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001307 O.PadToColumn(MAI->getCommentColumn());
1308 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001309 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001310 }
Evan Cheng11db8142009-03-24 00:17:40 +00001311 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001312 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001313 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001314 O.PadToColumn(MAI->getCommentColumn());
1315 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001316 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001317 }
Evan Cheng11db8142009-03-24 00:17:40 +00001318 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001319 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001320 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001321 O.PadToColumn(MAI->getCommentColumn());
1322 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001323 << " least significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001324 }
Evan Cheng11db8142009-03-24 00:17:40 +00001325 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001326 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001327 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001328 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001329 O.PadToColumn(MAI->getCommentColumn());
1330 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001331 << " least significant word of ppc_fp128";
Dan Gohman87581eb2009-08-12 18:32:22 +00001332 }
Evan Cheng11db8142009-03-24 00:17:40 +00001333 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001334 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001335 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001336 O.PadToColumn(MAI->getCommentColumn());
1337 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001338 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001339 }
Evan Cheng11db8142009-03-24 00:17:40 +00001340 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001341 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohman87581eb2009-08-12 18:32:22 +00001342 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001343 O.PadToColumn(MAI->getCommentColumn());
1344 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001345 << " next word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001346 }
Evan Cheng11db8142009-03-24 00:17:40 +00001347 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001348 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohman87581eb2009-08-12 18:32:22 +00001349 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001350 O.PadToColumn(MAI->getCommentColumn());
1351 O << MAI->getCommentString()
Dan Gohman2aa282f2009-08-13 01:36:44 +00001352 << " most significant word";
Dan Gohman87581eb2009-08-12 18:32:22 +00001353 }
Evan Cheng11db8142009-03-24 00:17:40 +00001354 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001355 }
1356 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001357 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001358}
1359
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001360void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1361 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001362 const TargetData *TD = TM.getTargetData();
1363 unsigned BitWidth = CI->getBitWidth();
Chris Lattner50340312010-01-13 04:39:46 +00001364 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001365
1366 // We don't expect assemblers to support integer data directives
1367 // for more than 64 bits, so we emit the data in at most 64-bit
1368 // quantities at a time.
1369 const uint64_t *RawData = CI->getValue().getRawData();
1370 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1371 uint64_t Val;
1372 if (TD->isBigEndian())
1373 Val = RawData[e - i - 1];
1374 else
1375 Val = RawData[i];
1376
Chris Lattner98910862010-01-13 04:38:16 +00001377 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001378 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner98910862010-01-13 04:38:16 +00001379 continue;
Dan Gohmane78b0c72008-12-22 21:14:27 +00001380 }
Chris Lattner98910862010-01-13 04:38:16 +00001381
1382 // Emit two 32-bit chunks, order depends on endianness.
1383 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1384 const char *FirstName = " least", *SecondName = " most";
1385 if (TD->isBigEndian()) {
1386 std::swap(FirstChunk, SecondChunk);
1387 std::swap(FirstName, SecondName);
1388 }
1389
1390 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1391 if (VerboseAsm) {
1392 O.PadToColumn(MAI->getCommentColumn());
1393 O << MAI->getCommentString()
1394 << FirstName << " significant half of i64 " << Val;
1395 }
1396 O << '\n';
1397
1398 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1399 if (VerboseAsm) {
1400 O.PadToColumn(MAI->getCommentColumn());
1401 O << MAI->getCommentString()
1402 << SecondName << " significant half of i64 " << Val;
1403 }
1404 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001405 }
1406}
1407
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001408/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001409void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001411 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001412 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413
Chris Lattner3b296362010-01-19 18:58:52 +00001414 if (CV->isNullValue() || isa<UndefValue>(CV))
1415 return EmitZeros(Size, AddrSpace);
Chris Lattner9d767d82010-01-13 04:34:19 +00001416
Chris Lattnere72333d2010-01-19 19:10:44 +00001417 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
1418 return EmitGlobalConstantArray(CVA, AddrSpace, *this);
Chris Lattner9d767d82010-01-13 04:34:19 +00001419
Chris Lattnere72333d2010-01-19 19:10:44 +00001420 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
1421 return EmitGlobalConstantStruct(CVS, AddrSpace, *this);
Chris Lattner9d767d82010-01-13 04:34:19 +00001422
Chris Lattnere72333d2010-01-19 19:10:44 +00001423 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
1424 return EmitGlobalConstantFP(CFP, AddrSpace);
Chris Lattner9d767d82010-01-13 04:34:19 +00001425
1426 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1427 // If we can directly emit an 8-byte constant, do it.
1428 if (Size == 8)
1429 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1430 O << Data64Dir << CI->getZExtValue() << '\n';
1431 return;
1432 }
1433
Dan Gohmane78b0c72008-12-22 21:14:27 +00001434 // Small integers are handled below; large integers are handled here.
1435 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001436 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001437 return;
1438 }
Chris Lattner9d767d82010-01-13 04:34:19 +00001439 }
1440
Chris Lattnere72333d2010-01-19 19:10:44 +00001441 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1442 return EmitGlobalConstantVector(V, AddrSpace, *this);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001443
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001444 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001446 if (VerboseAsm) {
1447 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1448 SmallString<40> S;
1449 CI->getValue().toStringUnsigned(S, 16);
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001450 O.PadToColumn(MAI->getCommentColumn());
1451 O << MAI->getCommentString() << " 0x" << S.str();
Evan Cheng11db8142009-03-24 00:17:40 +00001452 }
Scott Michele067c3c2008-06-03 15:39:51 +00001453 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001454 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001455}
1456
Chris Lattner89b36582008-08-17 07:19:36 +00001457void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001458 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001459 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001460}
1461
1462/// PrintSpecial - Print information related to the specified machine instr
1463/// that is independent of the operand, and may be independent of the instr
1464/// itself. This can be useful for portably encoding the comment character
1465/// or other bits of target-specific knowledge into the asmstrings. The
1466/// syntax used is ${:comment}. Targets can override this to add support
1467/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001468void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 if (!strcmp(Code, "private")) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001470 O << MAI->getPrivateGlobalPrefix();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001471 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001472 if (VerboseAsm)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001473 O << MAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001475 // Comparing the address of MI isn't sufficient, because machineinstrs may
1476 // be allocated to the same address across functions.
1477 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1478
Owen Andersonda3388b2009-06-24 22:28:12 +00001479 // If this is a new LastFn instruction, bump the counter.
1480 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 ++Counter;
1482 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001483 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001484 }
1485 O << Counter;
1486 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001487 std::string msg;
1488 raw_string_ostream Msg(msg);
1489 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001490 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001491 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 }
1493}
1494
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001495/// processDebugLoc - Processes the debug information of each machine
1496/// instruction's DebugLoc.
Devang Patel5450fc12009-10-06 02:19:11 +00001497void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1498 bool BeforePrintingInsn) {
Devang Patel90a0fe32009-11-10 23:06:00 +00001499 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1500 || !DW->ShouldEmitDwarfDebug())
Chris Lattner693cfcc2009-08-05 04:09:18 +00001501 return;
Devang Patel86049dc2009-09-30 23:12:50 +00001502 DebugLoc DL = MI->getDebugLoc();
Devang Patel90a0fe32009-11-10 23:06:00 +00001503 if (DL.isUnknown())
1504 return;
Devang Patel042945f2010-01-16 06:09:35 +00001505 DILocation CurDLT = MF->getDILocation(DL);
1506 if (CurDLT.getScope().isNull())
Devang Patel90a0fe32009-11-10 23:06:00 +00001507 return;
1508
1509 if (BeforePrintingInsn) {
Devang Patelc2103b12010-01-19 06:09:04 +00001510 if (CurDLT.getNode() != PrevDLT) {
Devang Patel042945f2010-01-16 06:09:35 +00001511 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1512 CurDLT.getColumnNumber(),
1513 CurDLT.getScope().getNode());
Devang Patel90a0fe32009-11-10 23:06:00 +00001514 printLabel(L);
Dan Gohmancfca6e32009-12-05 01:42:34 +00001515 O << '\n';
Devang Patel90a0fe32009-11-10 23:06:00 +00001516 DW->BeginScope(MI, L);
Devang Patelc2103b12010-01-19 06:09:04 +00001517 PrevDLT = CurDLT.getNode();
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001518 }
Devang Patel90a0fe32009-11-10 23:06:00 +00001519 } else {
1520 // After printing instruction
1521 DW->EndScope(MI);
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001522 }
1523}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524
Devang Patel90a0fe32009-11-10 23:06:00 +00001525
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001526/// printInlineAsm - This method formats and prints the specified machine
1527/// instruction that is an inline asm.
1528void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1529 unsigned NumOperands = MI->getNumOperands();
1530
1531 // Count the number of register definitions.
1532 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001533 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001534 ++NumDefs)
1535 assert(NumDefs != NumOperands-1 && "No asm string?");
1536
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001537 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001538
1539 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1540 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1541
Dan Gohmanbbc43962009-11-06 00:04:54 +00001542 O << '\t';
1543
Dale Johannesene99fc902008-01-29 02:21:21 +00001544 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1545 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001546 if (AsmStr[0] == 0) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001547 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1548 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 return;
1550 }
1551
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001552 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001553
1554 // The variant of the current asmprinter.
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001555 int AsmPrinterVariant = MAI->getAssemblerDialect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001556
1557 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1558 const char *LastEmitted = AsmStr; // One past the last character emitted.
1559
1560 while (*LastEmitted) {
1561 switch (*LastEmitted) {
1562 default: {
1563 // Not a special case, emit the string section literally.
1564 const char *LiteralEnd = LastEmitted+1;
1565 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1566 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1567 ++LiteralEnd;
1568 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1569 O.write(LastEmitted, LiteralEnd-LastEmitted);
1570 LastEmitted = LiteralEnd;
1571 break;
1572 }
1573 case '\n':
1574 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001575 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001576 break;
1577 case '$': {
1578 ++LastEmitted; // Consume '$' character.
1579 bool Done = true;
1580
1581 // Handle escapes.
1582 switch (*LastEmitted) {
1583 default: Done = false; break;
1584 case '$': // $$ -> $
1585 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1586 O << '$';
1587 ++LastEmitted; // Consume second '$' character.
1588 break;
1589 case '(': // $( -> same as GCC's { character.
1590 ++LastEmitted; // Consume '(' character.
1591 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001592 llvm_report_error("Nested variants found in inline asm string: '"
1593 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001594 }
1595 CurVariant = 0; // We're in the first variant now.
1596 break;
1597 case '|':
1598 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001599 if (CurVariant == -1)
1600 O << '|'; // this is gcc's behavior for | outside a variant
1601 else
1602 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 break;
1604 case ')': // $) -> same as GCC's } char.
1605 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001606 if (CurVariant == -1)
1607 O << '}'; // this is gcc's behavior for } outside a variant
1608 else
1609 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610 break;
1611 }
1612 if (Done) break;
1613
1614 bool HasCurlyBraces = false;
1615 if (*LastEmitted == '{') { // ${variable}
1616 ++LastEmitted; // Consume '{' character.
1617 HasCurlyBraces = true;
1618 }
1619
Chris Lattner6af48032009-03-10 05:37:13 +00001620 // If we have ${:foo}, then this is not a real operand reference, it is a
1621 // "magic" string reference, just like in .td files. Arrange to call
1622 // PrintSpecial.
1623 if (HasCurlyBraces && *LastEmitted == ':') {
1624 ++LastEmitted;
1625 const char *StrStart = LastEmitted;
1626 const char *StrEnd = strchr(StrStart, '}');
1627 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001628 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1629 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001630 }
1631
1632 std::string Val(StrStart, StrEnd);
1633 PrintSpecial(MI, Val.c_str());
1634 LastEmitted = StrEnd+1;
1635 break;
1636 }
1637
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001638 const char *IDStart = LastEmitted;
1639 char *IDEnd;
1640 errno = 0;
1641 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1642 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001643 llvm_report_error("Bad $ operand number in inline asm string: '"
1644 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 }
1646 LastEmitted = IDEnd;
1647
1648 char Modifier[2] = { 0, 0 };
1649
1650 if (HasCurlyBraces) {
1651 // If we have curly braces, check for a modifier character. This
1652 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1653 if (*LastEmitted == ':') {
1654 ++LastEmitted; // Consume ':' character.
1655 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001656 llvm_report_error("Bad ${:} expression in inline asm string: '"
1657 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001658 }
1659
1660 Modifier[0] = *LastEmitted;
1661 ++LastEmitted; // Consume modifier character.
1662 }
1663
1664 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001665 llvm_report_error("Bad ${} expression in inline asm string: '"
1666 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001667 }
1668 ++LastEmitted; // Consume '}' character.
1669 }
1670
1671 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001672 llvm_report_error("Invalid $ operand number in inline asm string: '"
1673 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001674 }
1675
1676 // Okay, we finally have a value number. Ask the target to print this
1677 // operand!
1678 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1679 unsigned OpNo = 1;
1680
1681 bool Error = false;
1682
1683 // Scan to find the machine operand number for the operand.
1684 for (; Val; --Val) {
1685 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001686 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001687 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001688 }
1689
1690 if (OpNo >= MI->getNumOperands()) {
1691 Error = true;
1692 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001693 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001694 ++OpNo; // Skip over the ID number.
1695
Chris Lattnerce409842010-01-17 21:43:43 +00001696 if (Modifier[0] == 'l') // labels are target independent
1697 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johannesencfb19e62007-11-05 21:20:28 +00001698 else {
1699 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001700 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001701 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1702 Modifier[0] ? Modifier : 0);
1703 } else {
1704 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1705 Modifier[0] ? Modifier : 0);
1706 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001707 }
1708 }
1709 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001710 std::string msg;
1711 raw_string_ostream Msg(msg);
Chris Lattnerce409842010-01-17 21:43:43 +00001712 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001713 MI->print(Msg);
1714 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001715 }
1716 }
1717 break;
1718 }
1719 }
1720 }
Chris Lattner32d4cc72009-09-09 23:14:36 +00001721 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001722}
1723
Evan Cheng3c0eda52008-03-15 00:03:38 +00001724/// printImplicitDef - This method prints the specified machine instruction
1725/// that is an implicit def.
1726void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001727 if (!VerboseAsm) return;
1728 O.PadToColumn(MAI->getCommentColumn());
1729 O << MAI->getCommentString() << " implicit-def: "
Chris Lattner28f7e352009-09-13 19:48:37 +00001730 << TRI->getName(MI->getOperand(0).getReg());
Evan Cheng3c0eda52008-03-15 00:03:38 +00001731}
1732
Jakob Stoklund Olesen7f251672009-11-04 19:24:37 +00001733void AsmPrinter::printKill(const MachineInstr *MI) const {
1734 if (!VerboseAsm) return;
1735 O.PadToColumn(MAI->getCommentColumn());
1736 O << MAI->getCommentString() << " kill:";
1737 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1738 const MachineOperand &op = MI->getOperand(n);
1739 assert(op.isReg() && "KILL instruction must have only register operands");
1740 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1741 }
1742}
1743
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001744/// printLabel - This method prints a local label used by debug and
1745/// exception handling tables.
1746void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001747 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001748}
1749
Evan Chenga53c40a2008-02-01 09:10:45 +00001750void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattner32d4cc72009-09-09 23:14:36 +00001751 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Chenga53c40a2008-02-01 09:10:45 +00001752}
1753
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001754/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1755/// instruction, using the specified assembler variant. Targets should
Dale Johannesenc90c30b2010-01-14 21:50:17 +00001756/// override this to format as appropriate.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001757bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1758 unsigned AsmVariant, const char *ExtraCode) {
1759 // Target doesn't support this yet!
1760 return true;
1761}
1762
1763bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1764 unsigned AsmVariant,
1765 const char *ExtraCode) {
1766 // Target doesn't support this yet!
1767 return true;
1768}
1769
Dan Gohman885793b2009-11-20 23:18:13 +00001770MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1771 const char *Suffix) const {
1772 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman91057512009-10-30 01:27:03 +00001773}
1774
1775MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman885793b2009-11-20 23:18:13 +00001776 const BasicBlock *BB,
1777 const char *Suffix) const {
Dan Gohman91057512009-10-30 01:27:03 +00001778 assert(BB->hasName() &&
1779 "Address of anonymous basic block not supported yet!");
1780
Dan Gohmanff592ec2009-11-05 23:14:35 +00001781 // This code must use the function name itself, and not the function number,
1782 // since it must be possible to generate the label name from within other
1783 // functions.
Chris Lattner6be13f32010-01-13 07:30:49 +00001784 SmallString<60> FnName;
1785 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman91057512009-10-30 01:27:03 +00001786
Chris Lattner6be13f32010-01-13 07:30:49 +00001787 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattnera38b2872010-01-13 06:38:18 +00001788 SmallString<60> NameResult;
Chris Lattner6be13f32010-01-13 07:30:49 +00001789 Mang->getNameWithPrefix(NameResult,
1790 StringRef("BA") + Twine((unsigned)FnName.size()) +
1791 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1792 Mangler::Private);
Dan Gohmanff592ec2009-11-05 23:14:35 +00001793
Chris Lattnera38b2872010-01-13 06:38:18 +00001794 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman91057512009-10-30 01:27:03 +00001795}
1796
Chris Lattner08c97082009-09-12 23:02:08 +00001797MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1798 SmallString<60> Name;
1799 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1800 << getFunctionNumber() << '_' << MBBID;
1801
1802 return OutContext.GetOrCreateSymbol(Name.str());
1803}
1804
Chris Lattner2eb781d2010-01-15 23:18:17 +00001805/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1806/// value.
1807MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1808 SmallString<60> NameStr;
1809 Mang->getNameWithPrefix(NameStr, GV, false);
1810 return OutContext.GetOrCreateSymbol(NameStr.str());
1811}
1812
Chris Lattnera6f2a102010-01-16 18:37:32 +00001813/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerf7871d12010-01-15 23:25:11 +00001814/// global value name as its base, with the specified suffix, and where the
Chris Lattnera6f2a102010-01-16 18:37:32 +00001815/// symbol is forced to have private linkage if ForcePrivate is true.
1816MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1817 StringRef Suffix,
1818 bool ForcePrivate) const {
Chris Lattnerf7871d12010-01-15 23:25:11 +00001819 SmallString<60> NameStr;
Chris Lattnera6f2a102010-01-16 18:37:32 +00001820 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerf7871d12010-01-15 23:25:11 +00001821 NameStr.append(Suffix.begin(), Suffix.end());
1822 return OutContext.GetOrCreateSymbol(NameStr.str());
1823}
1824
Chris Lattner2eb781d2010-01-15 23:18:17 +00001825/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1826/// ExternalSymbol.
1827MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1828 SmallString<60> NameStr;
1829 Mang->getNameWithPrefix(NameStr, Sym);
1830 return OutContext.GetOrCreateSymbol(NameStr.str());
1831}
1832
Chris Lattner08c97082009-09-12 23:02:08 +00001833
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001834/// EmitBasicBlockStart - This method prints the label for the specified
1835/// MachineBasicBlock, an alignment (if present) and a comment describing
1836/// it if appropriate.
Chris Lattnerda5fb6d2009-09-14 03:15:54 +00001837void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohman65e3b3e2009-10-30 01:34:35 +00001838 // Emit an alignment directive for this block, if needed.
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001839 if (unsigned Align = MBB->getAlignment())
1840 EmitAlignment(Log2_32(Align));
Evan Cheng45c1edb2008-02-28 00:43:03 +00001841
Dan Gohman65e3b3e2009-10-30 01:34:35 +00001842 // If the block has its address taken, emit a special label to satisfy
1843 // references to the block. This is done so that we don't need to
1844 // remember the number of this label, and so that we can make
1845 // forward references to labels without knowing what their numbers
1846 // will be.
Dan Gohman91057512009-10-30 01:27:03 +00001847 if (MBB->hasAddressTaken()) {
Chris Lattnerce409842010-01-17 21:43:43 +00001848 O << *GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1849 MBB->getBasicBlock());
Dan Gohman91057512009-10-30 01:27:03 +00001850 O << ':';
1851 if (VerboseAsm) {
1852 O.PadToColumn(MAI->getCommentColumn());
1853 O << MAI->getCommentString() << " Address Taken";
1854 }
1855 O << '\n';
1856 }
1857
Dan Gohman65e3b3e2009-10-30 01:34:35 +00001858 // Print the main label for the block.
Dan Gohman5fda4342009-10-06 17:38:38 +00001859 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1860 if (VerboseAsm)
1861 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1862 } else {
Chris Lattnerce409842010-01-17 21:43:43 +00001863 O << *GetMBBSymbol(MBB->getNumber()) << ':';
Dan Gohman5fda4342009-10-06 17:38:38 +00001864 if (!VerboseAsm)
1865 O << '\n';
1866 }
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001867
Dan Gohman65e3b3e2009-10-30 01:34:35 +00001868 // Print some comments to accompany the label.
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001869 if (VerboseAsm) {
Dan Gohman495af972009-08-12 18:47:05 +00001870 if (const BasicBlock *BB = MBB->getBasicBlock())
1871 if (BB->hasName()) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001872 O.PadToColumn(MAI->getCommentColumn());
1873 O << MAI->getCommentString() << ' ';
Dan Gohman5ee8d032009-08-12 20:56:56 +00001874 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohman495af972009-08-12 18:47:05 +00001875 }
David Greenee52fd872009-08-10 16:38:07 +00001876
Chris Lattner2faa4ef2009-09-13 18:25:37 +00001877 EmitComments(*MBB);
Dan Gohman5fda4342009-10-06 17:38:38 +00001878 O << '\n';
David Greenee52fd872009-08-10 16:38:07 +00001879 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001880}
1881
Evan Cheng6fb06762007-11-09 01:32:10 +00001882/// printPICJumpTableSetLabel - This method prints a set label for the
1883/// specified MachineBasicBlock for a jumptable entry.
1884void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1885 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001886 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887 return;
1888
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001889 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattnerce409842010-01-17 21:43:43 +00001890 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1891 << *GetMBBSymbol(MBB->getNumber())
1892 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001893 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001894}
1895
Evan Cheng6fb06762007-11-09 01:32:10 +00001896void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1897 const MachineBasicBlock *MBB) const {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001898 if (!MAI->getSetDirective())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001899 return;
1900
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001901 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001902 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattnerce409842010-01-17 21:43:43 +00001903 << "_set_" << MBB->getNumber() << ','
1904 << *GetMBBSymbol(MBB->getNumber())
1905 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng477013c2007-10-14 05:57:21 +00001906 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001907}
1908
1909/// printDataDirective - This method prints the asm directive for the
1910/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001911void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001912 const TargetData *TD = TM.getTargetData();
1913 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001914 case Type::FloatTyID: case Type::DoubleTyID:
1915 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1916 assert(0 && "Should have already output floating point constant.");
1917 default:
1918 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001919 case Type::IntegerTyID: {
1920 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1921 if (BitWidth <= 8)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001922 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001923 else if (BitWidth <= 16)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001924 O << MAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001925 else if (BitWidth <= 32)
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001926 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001927 else if (BitWidth <= 64) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001928 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001929 "Target cannot handle 64-bit constant exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001930 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001931 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001932 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001933 }
1934 break;
1935 }
1936 case Type::PointerTyID:
1937 if (TD->getPointerSize() == 8) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001938 assert(MAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001939 "Target cannot handle 64-bit pointer exprs!");
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001940 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001941 } else if (TD->getPointerSize() == 2) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001942 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001943 } else if (TD->getPointerSize() == 1) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001944 O << MAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001945 } else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00001946 O << MAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001947 }
1948 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001949 }
1950}
1951
Chris Lattner67cce682010-01-15 23:38:51 +00001952void AsmPrinter::printVisibility(const MCSymbol *Sym,
1953 unsigned Visibility) const {
1954 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattnerce409842010-01-17 21:43:43 +00001955 if (const char *Directive = MAI->getHiddenDirective())
1956 O << Directive << *Sym << '\n';
Chris Lattner67cce682010-01-15 23:38:51 +00001957 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattnerce409842010-01-17 21:43:43 +00001958 if (const char *Directive = MAI->getProtectedDirective())
1959 O << Directive << *Sym << '\n';
Chris Lattner67cce682010-01-15 23:38:51 +00001960 }
1961}
1962
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001963void AsmPrinter::printOffset(int64_t Offset) const {
1964 if (Offset > 0)
1965 O << '+' << Offset;
1966 else if (Offset < 0)
1967 O << Offset;
1968}
1969
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001970GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1971 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001972 return 0;
1973
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001974 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001975 if (GCPI != GCMetadataPrinters.end())
1976 return GCPI->second;
1977
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001978 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001979
1980 for (GCMetadataPrinterRegistry::iterator
1981 I = GCMetadataPrinterRegistry::begin(),
1982 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1983 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001984 GCMetadataPrinter *GMP = I->instantiate();
1985 GMP->S = S;
1986 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1987 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001988 }
1989
Chris Lattnerbf9d76d2009-08-23 07:19:13 +00001990 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001991 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001992}
David Greene63486122009-07-13 20:25:48 +00001993
1994/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001995void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greeneca9b04b2009-11-13 21:34:57 +00001996 if (!VerboseAsm)
1997 return;
David Greene4a37a692009-07-16 22:24:20 +00001998
David Greeneca9b04b2009-11-13 21:34:57 +00001999 bool Newline = false;
2000
2001 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel042945f2010-01-16 06:09:35 +00002002 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greeneca9b04b2009-11-13 21:34:57 +00002003
2004 // Print source line info.
2005 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman4698bab2009-12-05 00:23:29 +00002006 O << MAI->getCommentString() << ' ';
Devang Patel042945f2010-01-16 06:09:35 +00002007 DIScope Scope = DLT.getScope();
Dan Gohman4698bab2009-12-05 00:23:29 +00002008 // Omit the directory, because it's likely to be long and uninteresting.
2009 if (!Scope.isNull())
2010 O << Scope.getFilename();
2011 else
2012 O << "<unknown>";
Devang Patel042945f2010-01-16 06:09:35 +00002013 O << ':' << DLT.getLineNumber();
2014 if (DLT.getColumnNumber() != 0)
2015 O << ':' << DLT.getColumnNumber();
David Greeneca9b04b2009-11-13 21:34:57 +00002016 Newline = true;
David Greene4a37a692009-07-16 22:24:20 +00002017 }
David Greeneca9b04b2009-11-13 21:34:57 +00002018
David Greene2e7b9932009-11-16 15:12:23 +00002019 // Check for spills and reloads
2020 int FI;
2021
2022 const MachineFrameInfo *FrameInfo =
2023 MI.getParent()->getParent()->getFrameInfo();
2024
2025 // We assume a single instruction only has a spill or reload, not
2026 // both.
David Greene11a046f12009-12-04 22:46:04 +00002027 const MachineMemOperand *MMO;
David Greene2e7b9932009-11-16 15:12:23 +00002028 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
2029 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greene11a046f12009-12-04 22:46:04 +00002030 MMO = *MI.memoperands_begin();
David Greene2e7b9932009-11-16 15:12:23 +00002031 if (Newline) O << '\n';
2032 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmane26680b2009-12-04 23:19:55 +00002033 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greene2e7b9932009-11-16 15:12:23 +00002034 Newline = true;
2035 }
2036 }
David Greene11a046f12009-12-04 22:46:04 +00002037 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greene2e7b9932009-11-16 15:12:23 +00002038 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
2039 if (Newline) O << '\n';
2040 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmane26680b2009-12-04 23:19:55 +00002041 O << MAI->getCommentString() << ' '
2042 << MMO->getSize() << "-byte Folded Reload";
David Greene2e7b9932009-11-16 15:12:23 +00002043 Newline = true;
2044 }
2045 }
2046 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
2047 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greene11a046f12009-12-04 22:46:04 +00002048 MMO = *MI.memoperands_begin();
David Greene2e7b9932009-11-16 15:12:23 +00002049 if (Newline) O << '\n';
2050 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmane26680b2009-12-04 23:19:55 +00002051 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greene2e7b9932009-11-16 15:12:23 +00002052 Newline = true;
2053 }
2054 }
David Greene11a046f12009-12-04 22:46:04 +00002055 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greene2e7b9932009-11-16 15:12:23 +00002056 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
2057 if (Newline) O << '\n';
2058 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmane26680b2009-12-04 23:19:55 +00002059 O << MAI->getCommentString() << ' '
2060 << MMO->getSize() << "-byte Folded Spill";
David Greene2e7b9932009-11-16 15:12:23 +00002061 Newline = true;
2062 }
2063 }
2064
2065 // Check for spill-induced copies
2066 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2067 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
2068 SrcSubIdx, DstSubIdx)) {
2069 if (MI.getAsmPrinterFlag(ReloadReuse)) {
2070 if (Newline) O << '\n';
2071 O.PadToColumn(MAI->getCommentColumn());
2072 O << MAI->getCommentString() << " Reload Reuse";
David Greene2e7b9932009-11-16 15:12:23 +00002073 }
2074 }
David Greene63486122009-07-13 20:25:48 +00002075}
2076
David Greene066ed6a2009-08-18 19:22:55 +00002077/// PrintChildLoopComment - Print comments about child loops within
2078/// the loop for this basic block, with nesting.
2079///
2080static void PrintChildLoopComment(formatted_raw_ostream &O,
2081 const MachineLoop *loop,
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002082 const MCAsmInfo *MAI,
David Greene066ed6a2009-08-18 19:22:55 +00002083 int FunctionNumber) {
2084 // Add child loop information
2085 for(MachineLoop::iterator cl = loop->begin(),
2086 clend = loop->end();
2087 cl != clend;
2088 ++cl) {
2089 MachineBasicBlock *Header = (*cl)->getHeader();
2090 assert(Header && "No header for loop");
2091
2092 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002093 O.PadToColumn(MAI->getCommentColumn());
David Greene066ed6a2009-08-18 19:22:55 +00002094
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002095 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00002096 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00002097 << " Child Loop BB" << FunctionNumber << "_"
2098 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
2099
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002100 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greene066ed6a2009-08-18 19:22:55 +00002101 }
2102}
2103
David Greenee52fd872009-08-10 16:38:07 +00002104/// EmitComments - Pretty-print comments for basic blocks
David Greeneca9b04b2009-11-13 21:34:57 +00002105void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greene066ed6a2009-08-18 19:22:55 +00002106 if (VerboseAsm) {
David Greenee52fd872009-08-10 16:38:07 +00002107 // Add loop depth information
2108 const MachineLoop *loop = LI->getLoopFor(&MBB);
2109
2110 if (loop) {
2111 // Print a newline after bb# annotation.
2112 O << "\n";
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002113 O.PadToColumn(MAI->getCommentColumn());
2114 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greenee52fd872009-08-10 16:38:07 +00002115 << '\n';
2116
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002117 O.PadToColumn(MAI->getCommentColumn());
David Greenee52fd872009-08-10 16:38:07 +00002118
2119 MachineBasicBlock *Header = loop->getHeader();
2120 assert(Header && "No header for loop");
2121
2122 if (Header == &MBB) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002123 O << MAI->getCommentString() << " Loop Header";
2124 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greenee52fd872009-08-10 16:38:07 +00002125 }
2126 else {
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002127 O << MAI->getCommentString() << " Loop Header is BB"
David Greenee52fd872009-08-10 16:38:07 +00002128 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2129 }
2130
2131 if (loop->empty()) {
2132 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002133 O.PadToColumn(MAI->getCommentColumn());
2134 O << MAI->getCommentString() << " Inner Loop";
David Greenee52fd872009-08-10 16:38:07 +00002135 }
2136
2137 // Add parent loop information
2138 for (const MachineLoop *CurLoop = loop->getParentLoop();
2139 CurLoop;
2140 CurLoop = CurLoop->getParentLoop()) {
2141 MachineBasicBlock *Header = CurLoop->getHeader();
2142 assert(Header && "No header for loop");
2143
2144 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +00002145 O.PadToColumn(MAI->getCommentColumn());
2146 O << MAI->getCommentString();
Chris Lattnerebb8c082009-08-23 00:51:00 +00002147 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greene066ed6a2009-08-18 19:22:55 +00002148 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greenee52fd872009-08-10 16:38:07 +00002149 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2150 }
2151 }
2152 }
2153}