blob: 3e54f0992a5fad03707ffe5425d015ed44e542b7 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner45111d12010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000037#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000046#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000047using namespace llvm;
48
Evan Cheng42bf74b2009-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
Devang Patel19974732007-05-03 01:11:54 +000053char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000054AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000055 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000056 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000057 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000058
59 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000060 // FIXME: Pass instprinter to streamer.
61 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000062
Devang Patel6b61f582010-01-16 06:09:35 +000063 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-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}
Chris Lattnered138932005-12-13 06:32:10 +000071
Gordon Henriksenc317a602008-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 Lattner2b2954f2009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000079}
Chris Lattnered138932005-12-13 06:32:10 +000080
Chris Lattner38c39882009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerdabf07c2009-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 Henriksence224772008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000097}
98
Chris Lattnera80ba712004-08-16 23:15:22 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattnerc0dba722010-01-17 18:22:35 +0000104 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000105
Bob Wilson812209a2009-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 Espindola952b8392008-12-03 11:01:37 +0000108
Chris Lattner33adcfb2009-08-22 21:43:10 +0000109 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000110 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000111 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000112 a function came from. */
113 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
114 }
115
Bob Wilson812209a2009-09-30 22:06:26 +0000116 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
117 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-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 Lattner33adcfb2009-08-22 21:43:10 +0000120 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000121
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000122 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000123 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000124 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000125 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000126 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000127
Chris Lattnerb55e0682009-09-24 05:44:53 +0000128 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
129 if (MMI)
130 MMI->AnalyzeModule(M);
131 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000132 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000133 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000134
Chris Lattnera80ba712004-08-16 23:15:22 +0000135 return false;
136}
137
Chris Lattner48d64ba2010-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 Lattner74bfe212010-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 Lattner48d64ba2010-01-19 04:39:15 +0000157
Chris Lattner74bfe212010-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 Lattner9744d612010-01-19 05:51:42 +0000164 // Handle common and BSS local symbols (.lcomm).
165 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000166 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
167
Chris Lattner74bfe212010-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());
172 }
Chris Lattner9744d612010-01-19 05:51:42 +0000173 if (GVKind.isCommon()) {
174 // .comm _foo, 42, 4
175 O << MAI->getCOMMDirective() << *GVSym << ',' << Size;
176 if (MAI->getCOMMDirectiveTakesAlignment())
177 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << AlignLog) : AlignLog);
178 } else if (const char *LComm = MAI->getLCOMMDirective()) {
179 // .lcomm _foo, 42, 4
180 O << LComm << *GVSym << ',' << Size;
181 if (MAI->getLCOMMDirectiveTakesAlignment())
182 O << ',' << AlignLog;
183 } else {
184 // .local _foo
185 O << "\t.local\t" << *GVSym << '\n';
186 // .comm _foo, 42, 4
187 O << MAI->getCOMMDirective() << *GVSym << ',' << Size;
188 if (MAI->getCOMMDirectiveTakesAlignment())
189 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << AlignLog) : AlignLog);
190 }
Chris Lattner74bfe212010-01-19 05:38:33 +0000191 O << '\n';
192 return;
193 }
194
195 const MCSection *TheSection =
196 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
197
198 // Handle the zerofill directive on darwin, which is a special form of BSS
199 // emission.
200 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
201 // .globl _foo
202 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
203 // .zerofill __DATA, __common, _foo, 400, 5
204 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
205 return;
206 }
207
208 OutStreamer.SwitchSection(TheSection);
209
210 // TODO: Factor into an 'emit linkage' thing that is shared with function
211 // bodies.
212 switch (GV->getLinkage()) {
213 case GlobalValue::CommonLinkage:
214 case GlobalValue::LinkOnceAnyLinkage:
215 case GlobalValue::LinkOnceODRLinkage:
216 case GlobalValue::WeakAnyLinkage:
217 case GlobalValue::WeakODRLinkage:
218 case GlobalValue::LinkerPrivateLinkage:
219 if (const char *WeakDef = MAI->getWeakDefDirective()) {
220 // .globl _foo
221 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
222 // .weak_definition _foo
223 O << WeakDef << *GVSym << '\n';
224 } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
225 // .globl _foo
226 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
227 // .linkonce same_size
228 O << LinkOnce;
229 } else
230 O << "\t.weak\t" << *GVSym << '\n';
231 break;
232 case GlobalValue::DLLExportLinkage:
233 case GlobalValue::AppendingLinkage:
234 // FIXME: appending linkage variables should go into a section of
235 // their name or something. For now, just emit them as external.
236 case GlobalValue::ExternalLinkage:
237 // If external or appending, declare as a global symbol.
238 // .globl _foo
239 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
240 break;
241 case GlobalValue::PrivateLinkage:
242 case GlobalValue::InternalLinkage:
243 break;
244 default:
245 llvm_unreachable("Unknown linkage type!");
246 }
247
248 EmitAlignment(AlignLog, GV);
249 O << *GVSym << ":";
250 if (VerboseAsm) {
251 O.PadToColumn(MAI->getCommentColumn());
252 O << MAI->getCommentString() << ' ';
253 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
254 }
255 O << '\n';
256
257 EmitGlobalConstant(GV->getInitializer());
258
259 if (MAI->hasDotTypeDotSizeDirective())
260 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
Chris Lattner48d64ba2010-01-19 04:39:15 +0000261}
262
263
Chris Lattnera80ba712004-08-16 23:15:22 +0000264bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000265 // Emit global variables.
266 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
267 I != E; ++I)
Chris Lattner48d64ba2010-01-19 04:39:15 +0000268 EmitGlobalVariable(I);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000269
Chris Lattner1f522fe2009-06-24 18:54:37 +0000270 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000271 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000272 DW->EndModule();
273
Chris Lattner0a7befa2009-06-24 18:52:01 +0000274 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000275 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000276 // FIXME: This is not lazy, it would be nice to only print weak references
277 // to stuff that is actually used. Note that doing so would require targets
278 // to notice uses in operands (due to constant exprs etc). This should
279 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000280
Chris Lattner0a7befa2009-06-24 18:52:01 +0000281 // Print out module-level global variables here.
282 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
283 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000284 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000285 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000286 }
287
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000288 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000289 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000290 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000291 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000292 }
293
Chris Lattner33adcfb2009-08-22 21:43:10 +0000294 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000295 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000296 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000297 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000298 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000299
300 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000301 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000302
Chris Lattner10b318b2010-01-17 21:43:43 +0000303 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
304 O << "\t.globl\t" << *Name << '\n';
305 else if (I->hasWeakLinkage())
306 O << MAI->getWeakRefDirective() << *Name << '\n';
307 else
Chris Lattner10595492010-01-16 01:37:14 +0000308 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000309
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000310 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000311
Chris Lattner10b318b2010-01-17 21:43:43 +0000312 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000313 }
314 }
315
Duncan Sands1465d612009-01-28 13:14:17 +0000316 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000317 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
318 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
319 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000320 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000321
Dan Gohmana779a982008-05-05 00:28:39 +0000322 // If we don't have any trampolines, then we don't require stack memory
323 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000324 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000325 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000326 if (MAI->getNonexecutableStackDirective())
327 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000328
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000329
330 // Allow the target to emit any magic that it wants at the end of the file,
331 // after everything else has gone out.
332 EmitEndOfAsmFile(M);
333
Chris Lattnera80ba712004-08-16 23:15:22 +0000334 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000335 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000336
337 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000338 return false;
339}
340
Chris Lattner25045bd2005-11-21 07:51:36 +0000341void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000342 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000343 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000344 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000345
Chris Lattner25d812b2009-09-16 00:35:39 +0000346 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000347 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000348}
349
Evan Cheng1606e8e2009-03-13 07:51:59 +0000350namespace {
351 // SectionCPs - Keep track the alignment, constpool entries per Section.
352 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000353 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000354 unsigned Alignment;
355 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000356 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000357 };
358}
359
Chris Lattner3b4fd322005-11-21 08:25:09 +0000360/// EmitConstantPool - Print to the current output stream assembly
361/// representations of the constants in the constant pool MCP. This is
362/// used to print out constants which have been "spilled to memory" by
363/// the code generator.
364///
365void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000366 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000367 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000368
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000369 // Calculate sections for constant pool entries. We collect entries to go into
370 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000371 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000372 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000373 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000374 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000375
376 SectionKind Kind;
377 switch (CPE.getRelocationInfo()) {
378 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000379 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000380 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000381 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000382 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000383 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000384 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000385 case 4: Kind = SectionKind::getMergeableConst4(); break;
386 case 8: Kind = SectionKind::getMergeableConst8(); break;
387 case 16: Kind = SectionKind::getMergeableConst16();break;
388 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000389 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000390 }
391
Chris Lattner83d77fa2009-08-01 23:46:12 +0000392 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000393
Evan Cheng1606e8e2009-03-13 07:51:59 +0000394 // The number of sections are small, just do a linear search from the
395 // last section to the first.
396 bool Found = false;
397 unsigned SecIdx = CPSections.size();
398 while (SecIdx != 0) {
399 if (CPSections[--SecIdx].S == S) {
400 Found = true;
401 break;
402 }
403 }
404 if (!Found) {
405 SecIdx = CPSections.size();
406 CPSections.push_back(SectionCPs(S, Align));
407 }
408
409 if (Align > CPSections[SecIdx].Alignment)
410 CPSections[SecIdx].Alignment = Align;
411 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000412 }
413
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000414 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000415 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000416 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000417 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000418
Evan Cheng1606e8e2009-03-13 07:51:59 +0000419 unsigned Offset = 0;
420 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
421 unsigned CPI = CPSections[i].CPEs[j];
422 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000423
Chris Lattner3029f922006-02-09 04:46:04 +0000424 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000425 unsigned AlignMask = CPE.getAlignment() - 1;
426 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
427 EmitZeros(NewOffset - Offset);
428
429 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000430 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000431
Chris Lattner33adcfb2009-08-22 21:43:10 +0000432 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000433 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000434 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000435 O.PadToColumn(MAI->getCommentColumn());
436 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000437 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000438 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000439 O << '\n';
440 if (CPE.isMachineConstantPoolEntry())
441 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
442 else
443 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000444 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000445 }
446}
447
Nate Begeman37efe672006-04-22 18:53:45 +0000448/// EmitJumpTableInfo - Print assembly representations of the jump tables used
449/// by the current function to the current output stream.
450///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000451void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
452 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000453 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
454 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000455
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000456 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000457
Nate Begeman2f1ae882006-07-27 01:13:04 +0000458 // Pick the directive to use to print the jump table entries, and switch to
459 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000460 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000461
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000462 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000463 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000464 if (F->isWeakForLinker() ||
465 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000466 // In PIC mode, we need to emit the jump table to the same section as the
467 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000468 // We should also do if the section name is NULL or function is declared in
469 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000470 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
471 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000472 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000473 // Otherwise, drop it in the readonly section.
474 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000475 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000476 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000477 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000478 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000479
480 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000481
Nate Begeman37efe672006-04-22 18:53:45 +0000482 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000483 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000484
485 // If this jump table was deleted, ignore it.
486 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000487
488 // For PIC codegen, if possible we want to use the SetDirective to reduce
489 // the number of relocations the assembler will generate for the jump table.
490 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000491 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000492 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000493 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000494 if (EmittedSets.insert(JTBBs[ii]))
495 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000496
Chris Lattner7c301912009-09-13 19:02:16 +0000497 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000498 // before each jump table. The first label is never referenced, but tells
499 // the assembler and linker the extents of the jump table object. The
500 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000501 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
502 O << MAI->getLinkerPrivateGlobalPrefix()
503 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000504 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000505
Chris Lattner33adcfb2009-08-22 21:43:10 +0000506 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000507 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000508
Nate Begeman37efe672006-04-22 18:53:45 +0000509 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000510 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000511 O << '\n';
512 }
513 }
514}
515
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000516void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
517 const MachineBasicBlock *MBB,
518 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000519 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000520
521 // Use JumpTableDirective otherwise honor the entry size from the jump table
522 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000523 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000524 bool HadJTEntryDirective = JTEntryDirective != NULL;
525 if (!HadJTEntryDirective) {
526 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000527 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000528 }
529
530 O << JTEntryDirective << ' ';
531
532 // If we have emitted set directives for the jump table entries, print
533 // them rather than the entries themselves. If we're emitting PIC, then
534 // emit the table entries as differences between two text section labels.
535 // If we're emitting non-PIC code, then emit the entries as direct
536 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000537 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000538 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000539 } else if (MAI->getSetDirective()) {
540 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000541 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000542 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000543 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000544 // If the arch uses custom Jump Table directives, don't calc relative to
545 // JT
546 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000547 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000548 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000549 }
550}
551
552
Chris Lattnered138932005-12-13 06:32:10 +0000553/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
554/// special global used by LLVM. If so, emit it and return true, otherwise
555/// do nothing and return false.
556bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000557 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000558 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000559 EmitLLVMUsedList(GV->getInitializer());
560 return true;
561 }
562
Chris Lattner401e10c2009-07-20 06:14:25 +0000563 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000564 if (GV->getSection() == "llvm.metadata" ||
565 GV->hasAvailableExternallyLinkage())
566 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000567
568 if (!GV->hasAppendingLinkage()) return false;
569
570 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000571
Evan Cheng916d07c2007-06-04 20:39:18 +0000572 const TargetData *TD = TM.getTargetData();
573 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000574 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000575 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000576 EmitAlignment(Align, 0);
577 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000578
579 if (TM.getRelocationModel() == Reloc::Static &&
580 MAI->hasStaticCtorDtorReferenceInStaticMode())
581 O << ".reference .constructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000582 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000583 }
584
Chris Lattnerf231c072009-03-09 05:52:15 +0000585 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000586 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000587 EmitAlignment(Align, 0);
588 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000589
590 if (TM.getRelocationModel() == Reloc::Static &&
591 MAI->hasStaticCtorDtorReferenceInStaticMode())
592 O << ".reference .destructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000593 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000594 }
595
596 return false;
597}
598
Chris Lattner33adcfb2009-08-22 21:43:10 +0000599/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000600/// global in the specified llvm.used list for which emitUsedDirectiveFor
601/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000602void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000603 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000604
Dan Gohmana119de82009-06-14 23:30:43 +0000605 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000606 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
607 if (InitList == 0) return;
608
609 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000610 const GlobalValue *GV =
611 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000612 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000613 O << Directive;
614 EmitConstantValueOnly(InitList->getOperand(i));
615 O << '\n';
616 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000617 }
618}
619
Chris Lattnered138932005-12-13 06:32:10 +0000620/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
621/// function pointers, ignoring the init priority.
622void AsmPrinter::EmitXXStructorList(Constant *List) {
623 // Should be an array of '{ int, void ()* }' structs. The first value is the
624 // init priority, which we ignore.
625 if (!isa<ConstantArray>(List)) return;
626 ConstantArray *InitList = cast<ConstantArray>(List);
627 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
628 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
629 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000630
631 if (CS->getOperand(1)->isNullValue())
632 return; // Found a null terminator, exit printing.
633 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000634 EmitGlobalConstant(CS->getOperand(1));
635 }
636}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000637
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000638
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000639//===----------------------------------------------------------------------===//
640/// LEB 128 number encoding.
641
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000642/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000643/// representing an unsigned leb128 value.
644void AsmPrinter::PrintULEB128(unsigned Value) const {
645 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000646 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000647 Value >>= 7;
648 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000649 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000650 if (Value) O << ", ";
651 } while (Value);
652}
653
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000654/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000655/// representing a signed leb128 value.
656void AsmPrinter::PrintSLEB128(int Value) const {
657 int Sign = Value >> (8 * sizeof(Value) - 1);
658 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000659
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000660 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000661 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000662 Value >>= 7;
663 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
664 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000665 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000666 if (IsMore) O << ", ";
667 } while (IsMore);
668}
669
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000670//===--------------------------------------------------------------------===//
671// Emission and print routines
672//
673
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000674/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000675///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000676void AsmPrinter::PrintHex(uint64_t Value) const {
677 O << "0x";
678 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000679}
680
681/// EOL - Print a newline character to asm stream. If a comment is present
682/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000683void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000684 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000685}
Owen Anderson25995092008-07-01 21:16:27 +0000686
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000687void AsmPrinter::EOL(const Twine &Comment) const {
688 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000689 O.PadToColumn(MAI->getCommentColumn());
690 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000691 << ' '
692 << Comment;
693 }
694 O << '\n';
695}
696
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000697static const char *DecodeDWARFEncoding(unsigned Encoding) {
698 switch (Encoding) {
699 case dwarf::DW_EH_PE_absptr:
700 return "absptr";
701 case dwarf::DW_EH_PE_omit:
702 return "omit";
703 case dwarf::DW_EH_PE_pcrel:
704 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000705 case dwarf::DW_EH_PE_udata4:
706 return "udata4";
707 case dwarf::DW_EH_PE_udata8:
708 return "udata8";
709 case dwarf::DW_EH_PE_sdata4:
710 return "sdata4";
711 case dwarf::DW_EH_PE_sdata8:
712 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000713 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
714 return "pcrel udata4";
715 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
716 return "pcrel sdata4";
717 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
718 return "pcrel udata8";
719 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
720 return "pcrel sdata8";
721 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
722 return "indirect pcrel udata4";
723 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
724 return "indirect pcrel sdata4";
725 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
726 return "indirect pcrel udata8";
727 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
728 return "indirect pcrel sdata8";
729 }
730
731 return 0;
732}
733
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000734void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
735 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000736 O.PadToColumn(MAI->getCommentColumn());
737 O << MAI->getCommentString()
738 << ' '
739 << Comment;
740
741 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
742 O << " (" << EncStr << ')';
743 }
744 O << '\n';
745}
746
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000747/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
748/// unsigned leb128 value.
749void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000750 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000751 O << "\t.uleb128\t"
752 << Value;
753 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000754 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000755 PrintULEB128(Value);
756 }
757}
758
759/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
760/// signed leb128 value.
761void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000762 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000763 O << "\t.sleb128\t"
764 << Value;
765 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000766 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000767 PrintSLEB128(Value);
768 }
769}
770
771/// EmitInt8 - Emit a byte directive and value.
772///
773void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000774 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000775 PrintHex(Value & 0xFF);
776}
777
778/// EmitInt16 - Emit a short directive and value.
779///
780void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000781 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000782 PrintHex(Value & 0xFFFF);
783}
784
785/// EmitInt32 - Emit a long directive and value.
786///
787void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000788 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000789 PrintHex(Value);
790}
791
792/// EmitInt64 - Emit a long long directive and value.
793///
794void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000795 if (MAI->getData64bitsDirective()) {
796 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000797 PrintHex(Value);
798 } else {
799 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000800 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000801 EmitInt32(unsigned(Value));
802 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000803 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000804 EmitInt32(unsigned(Value >> 32));
805 }
806 }
807}
808
809/// toOctal - Convert the low order bits of X into an octal digit.
810///
811static inline char toOctal(int X) {
812 return (X&7)+'0';
813}
814
815/// printStringChar - Print a char, escaped if necessary.
816///
David Greene71847812009-07-14 20:18:05 +0000817static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000818 if (C == '"') {
819 O << "\\\"";
820 } else if (C == '\\') {
821 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000822 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000823 O << C;
824 } else {
825 switch(C) {
826 case '\b': O << "\\b"; break;
827 case '\f': O << "\\f"; break;
828 case '\n': O << "\\n"; break;
829 case '\r': O << "\\r"; break;
830 case '\t': O << "\\t"; break;
831 default:
832 O << '\\';
833 O << toOctal(C >> 6);
834 O << toOctal(C >> 3);
835 O << toOctal(C >> 0);
836 break;
837 }
838 }
839}
840
841/// EmitString - Emit a string with quotes and a null terminator.
842/// Special characters are emitted properly.
843/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000844void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000845 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000846}
847
848void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000849 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000850 if (AscizDirective)
851 O << AscizDirective;
852 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000853 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000854 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000855 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000856 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000857 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000858 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000859 else
860 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000861}
862
863
Dan Gohman189f80d2007-09-24 20:58:13 +0000864/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000865void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000866 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000867 for (unsigned i = 0, N = Name.size(); i < N; ++i)
868 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000869 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000870}
871
872
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000873//===----------------------------------------------------------------------===//
874
Chris Lattner3a420532007-05-31 18:57:45 +0000875// EmitAlignment - Emit an alignment directive to the specified power of
876// two boundary. For example, if you pass in 3 here, you will get an 8
877// byte alignment. If a global value is specified, and if that global has
878// an explicit alignment requested, it will unconditionally override the
879// alignment request. However, if ForcedAlignBits is specified, this value
880// has final say: the ultimate alignment will be the max of ForcedAlignBits
881// and the alignment computed with NumBits and the global.
882//
883// The algorithm is:
884// Align = NumBits;
885// if (GV && GV->hasalignment) Align = GV->getalignment();
886// Align = std::max(Align, ForcedAlignBits);
887//
888void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000889 unsigned ForcedAlignBits,
890 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000891 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000892 NumBits = Log2_32(GV->getAlignment());
893 NumBits = std::max(NumBits, ForcedAlignBits);
894
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000895 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000896
897 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000898 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000899 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000900
901 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000902}
David Greenea5bb59f2009-08-05 21:00:52 +0000903
Chris Lattner25045bd2005-11-21 07:51:36 +0000904/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000905///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000906void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000907 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000908 if (MAI->getZeroDirective()) {
909 O << MAI->getZeroDirective() << NumZeros;
910 if (MAI->getZeroDirectiveSuffix())
911 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000912 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000913 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000914 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000915 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000916 }
917 }
918}
919
Chris Lattnera80ba712004-08-16 23:15:22 +0000920// Print out the specified constant, without a storage class. Only the
921// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000922void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000923 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000924 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000925 return;
926 }
927
928 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000929 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000930 return;
931 }
932
933 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000934 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000935 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000936 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000937 return;
938 }
939
940 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000941 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000942 return;
943 }
944
945 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
946 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000947 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000948 O << '0';
949 return;
950 }
951
952 switch (CE->getOpcode()) {
953 case Instruction::ZExt:
954 case Instruction::SExt:
955 case Instruction::FPTrunc:
956 case Instruction::FPExt:
957 case Instruction::UIToFP:
958 case Instruction::SIToFP:
959 case Instruction::FPToUI:
960 case Instruction::FPToSI:
961 default:
962 llvm_unreachable("FIXME: Don't support this constant cast expr");
963 case Instruction::GetElementPtr: {
964 // generate a symbolic expression for the byte address
965 const TargetData *TD = TM.getTargetData();
966 const Constant *ptrVal = CE->getOperand(0);
967 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
968 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
969 idxVec.size());
970 if (Offset == 0)
971 return EmitConstantValueOnly(ptrVal);
972
973 // Truncate/sext the offset to the pointer size.
974 if (TD->getPointerSizeInBits() != 64) {
975 int SExtAmount = 64-TD->getPointerSizeInBits();
976 Offset = (Offset << SExtAmount) >> SExtAmount;
977 }
978
979 if (Offset)
980 O << '(';
981 EmitConstantValueOnly(ptrVal);
982 if (Offset > 0)
983 O << ") + " << Offset;
984 else
985 O << ") - " << -Offset;
986 return;
987 }
988 case Instruction::BitCast:
989 return EmitConstantValueOnly(CE->getOperand(0));
990
991 case Instruction::IntToPtr: {
992 // Handle casts to pointers by changing them into casts to the appropriate
993 // integer type. This promotes constant folding and simplifies this code.
994 const TargetData *TD = TM.getTargetData();
995 Constant *Op = CE->getOperand(0);
996 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
997 false/*ZExt*/);
998 return EmitConstantValueOnly(Op);
999 }
1000
1001 case Instruction::PtrToInt: {
1002 // Support only foldable casts to/from pointers that can be eliminated by
1003 // changing the pointer to the appropriately sized integer type.
1004 Constant *Op = CE->getOperand(0);
1005 const Type *Ty = CE->getType();
1006 const TargetData *TD = TM.getTargetData();
1007
1008 // We can emit the pointer value into this slot if the slot is an
1009 // integer slot greater or equal to the size of the pointer.
1010 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
1011 return EmitConstantValueOnly(Op);
1012
1013 O << "((";
1014 EmitConstantValueOnly(Op);
1015 APInt ptrMask =
1016 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
1017
1018 SmallString<40> S;
1019 ptrMask.toStringUnsigned(S);
1020 O << ") & " << S.str() << ')';
1021 return;
1022 }
1023
1024 case Instruction::Trunc:
1025 // We emit the value and depend on the assembler to truncate the generated
1026 // expression properly. This is important for differences between
1027 // blockaddress labels. Since the two labels are in the same function, it
1028 // is reasonable to treat their delta as a 32-bit value.
1029 return EmitConstantValueOnly(CE->getOperand(0));
1030
1031 case Instruction::Add:
1032 case Instruction::Sub:
1033 case Instruction::And:
1034 case Instruction::Or:
1035 case Instruction::Xor:
1036 O << '(';
1037 EmitConstantValueOnly(CE->getOperand(0));
1038 O << ')';
1039 switch (CE->getOpcode()) {
1040 case Instruction::Add:
1041 O << " + ";
1042 break;
1043 case Instruction::Sub:
1044 O << " - ";
1045 break;
1046 case Instruction::And:
1047 O << " & ";
1048 break;
1049 case Instruction::Or:
1050 O << " | ";
1051 break;
1052 case Instruction::Xor:
1053 O << " ^ ";
1054 break;
1055 default:
1056 break;
1057 }
1058 O << '(';
1059 EmitConstantValueOnly(CE->getOperand(1));
1060 O << ')';
1061 break;
Chris Lattnera80ba712004-08-16 23:15:22 +00001062 }
1063}
Chris Lattner1b7e2352004-08-17 06:36:49 +00001064
Chris Lattner2980cef2005-11-10 18:06:33 +00001065/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +00001066/// the predicate isString is true.
1067///
David Greene71847812009-07-14 20:18:05 +00001068static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +00001069 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +00001070 assert(CVA->isString() && "Array is not string compatible!");
1071
Dan Gohmand19a53b2008-06-30 22:03:41 +00001072 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +00001073 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001074 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +00001075 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001076 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001077 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001078 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001079}
1080
Jeff Cohenc884db42006-05-02 01:16:28 +00001081/// EmitString - Emit a zero-byte-terminated string constant.
1082///
1083void AsmPrinter::EmitString(const ConstantArray *CVA) const {
1084 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001085 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +00001086 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001087 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001088 printAsCString(O, CVA, NumElts-1);
1089 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001090 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001091 printAsCString(O, CVA, NumElts);
1092 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001093 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +00001094}
1095
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001096void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
1097 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001098 if (CVA->isString()) {
1099 EmitString(CVA);
1100 } else { // Not a string. Print the values in successive locations
1101 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001102 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001103 }
1104}
1105
1106void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1107 const VectorType *PTy = CP->getType();
1108
1109 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1110 EmitGlobalConstant(CP->getOperand(I));
1111}
1112
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001113void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1114 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001115 // Print the fields in successive locations. Pad to align if needed!
1116 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001117 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001118 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1119 uint64_t sizeSoFar = 0;
1120 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1121 const Constant* field = CVS->getOperand(i);
1122
1123 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001124 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001125 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1126 - cvsLayout->getElementOffset(i)) - fieldSize;
1127 sizeSoFar += fieldSize + padSize;
1128
1129 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001130 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001131
1132 // Insert padding - this may include padding to increase the size of the
1133 // current field up to the ABI size (if the struct is not packed) as well
1134 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001135 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001136 }
1137 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1138 "Layout of constant struct may be incorrect!");
1139}
1140
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001141void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1142 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001143 // FP Constants are printed as integer constants to avoid losing
1144 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001145 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001146 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001147 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001148 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1149 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001150 if (MAI->getData64bitsDirective(AddrSpace)) {
1151 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001152 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001153 O.PadToColumn(MAI->getCommentColumn());
1154 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001155 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001156 O << '\n';
1157 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001158 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001159 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001160 O.PadToColumn(MAI->getCommentColumn());
1161 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001162 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001163 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001164 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001165 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001166 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001167 O.PadToColumn(MAI->getCommentColumn());
1168 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001169 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001170 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001171 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001172 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001173 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001174 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001175 O.PadToColumn(MAI->getCommentColumn());
1176 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001177 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001178 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001179 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001180 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001181 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001182 O.PadToColumn(MAI->getCommentColumn());
1183 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001184 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001185 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001186 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001187 }
1188 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001189 }
1190
1191 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001192 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001193 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001194 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001195 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O.PadToColumn(MAI->getCommentColumn());
1197 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001198 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001199 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001200 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001201 }
1202
1203 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001204 // all long double variants are printed as hex
1205 // api needed to prevent premature destruction
1206 APInt api = CFP->getValueAPF().bitcastToAPInt();
1207 const uint64_t *p = api.getRawData();
1208 // Convert to double so we can print the approximate val as a comment.
1209 APFloat DoubleVal = CFP->getValueAPF();
1210 bool ignored;
1211 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1212 &ignored);
1213 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001214 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001215 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001216 O.PadToColumn(MAI->getCommentColumn());
1217 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001218 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001219 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001220 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001221 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001222 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001223 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001224 O.PadToColumn(MAI->getCommentColumn());
1225 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001226 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001227 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001228 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001229 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001230 O.PadToColumn(MAI->getCommentColumn());
1231 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001232 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001233 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001234 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001235 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001236 O.PadToColumn(MAI->getCommentColumn());
1237 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001238 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001239 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001240 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001241 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001242 O.PadToColumn(MAI->getCommentColumn());
1243 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001244 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001245 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001246 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001247 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001248 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001249 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001250 O.PadToColumn(MAI->getCommentColumn());
1251 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001252 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001253 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001254 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001255 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001256 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001257 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001258 O.PadToColumn(MAI->getCommentColumn());
1259 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001260 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001261 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001262 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001263 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001264 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001265 O.PadToColumn(MAI->getCommentColumn());
1266 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001267 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001268 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001269 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001270 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001271 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001272 O.PadToColumn(MAI->getCommentColumn());
1273 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001274 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001275 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001276 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001277 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001278 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001279 O.PadToColumn(MAI->getCommentColumn());
1280 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001281 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001282 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001283 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001284 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001285 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1286 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001287 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001288 }
1289
1290 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001291 // all long double variants are printed as hex
1292 // api needed to prevent premature destruction
1293 APInt api = CFP->getValueAPF().bitcastToAPInt();
1294 const uint64_t *p = api.getRawData();
1295 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001296 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001297 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001298 O.PadToColumn(MAI->getCommentColumn());
1299 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001300 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001301 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001302 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001303 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001304 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001305 O.PadToColumn(MAI->getCommentColumn());
1306 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001307 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001308 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001309 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001310 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001311 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001312 O.PadToColumn(MAI->getCommentColumn());
1313 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001314 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001315 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001316 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001317 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001318 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001319 O.PadToColumn(MAI->getCommentColumn());
1320 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001321 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001322 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001323 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001324 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001325 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001326 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001327 O.PadToColumn(MAI->getCommentColumn());
1328 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001329 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001330 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001331 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001332 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001333 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001334 O.PadToColumn(MAI->getCommentColumn());
1335 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001336 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001337 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001338 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001339 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001340 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001341 O.PadToColumn(MAI->getCommentColumn());
1342 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001343 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001344 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001345 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001346 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001347 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001348 O.PadToColumn(MAI->getCommentColumn());
1349 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001350 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001351 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001352 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001353 }
1354 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001355 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001356}
1357
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001358void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1359 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001360 const TargetData *TD = TM.getTargetData();
1361 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001362 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001363
1364 // We don't expect assemblers to support integer data directives
1365 // for more than 64 bits, so we emit the data in at most 64-bit
1366 // quantities at a time.
1367 const uint64_t *RawData = CI->getValue().getRawData();
1368 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1369 uint64_t Val;
1370 if (TD->isBigEndian())
1371 Val = RawData[e - i - 1];
1372 else
1373 Val = RawData[i];
1374
Chris Lattner5979dff2010-01-13 04:38:16 +00001375 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001376 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001377 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001378 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001379
1380 // Emit two 32-bit chunks, order depends on endianness.
1381 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1382 const char *FirstName = " least", *SecondName = " most";
1383 if (TD->isBigEndian()) {
1384 std::swap(FirstChunk, SecondChunk);
1385 std::swap(FirstName, SecondName);
1386 }
1387
1388 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1389 if (VerboseAsm) {
1390 O.PadToColumn(MAI->getCommentColumn());
1391 O << MAI->getCommentString()
1392 << FirstName << " significant half of i64 " << Val;
1393 }
1394 O << '\n';
1395
1396 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1397 if (VerboseAsm) {
1398 O.PadToColumn(MAI->getCommentColumn());
1399 O << MAI->getCommentString()
1400 << SecondName << " significant half of i64 " << Val;
1401 }
1402 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001403 }
1404}
1405
Chris Lattner25045bd2005-11-21 07:51:36 +00001406/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001407void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001408 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001409 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001410 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001411
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001412 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001413 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001414 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001415 }
1416
1417 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001418 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001419 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001420 }
1421
1422 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001423 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001424 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001425 }
1426
1427 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001428 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001429 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001430 }
1431
1432 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1433 // If we can directly emit an 8-byte constant, do it.
1434 if (Size == 8)
1435 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1436 O << Data64Dir << CI->getZExtValue() << '\n';
1437 return;
1438 }
1439
Dan Gohman00d448a2008-12-22 21:14:27 +00001440 // Small integers are handled below; large integers are handled here.
1441 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001442 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001443 return;
1444 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001445 }
1446
1447 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001448 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001449 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001450 }
1451
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001452 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001453 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001454 if (VerboseAsm) {
1455 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1456 SmallString<40> S;
1457 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001458 O.PadToColumn(MAI->getCommentColumn());
1459 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001460 }
Scott Micheleefc8452008-06-03 15:39:51 +00001461 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001462 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001463}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001464
Chris Lattnerfad86b02008-08-17 07:19:36 +00001465void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001466 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001467 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001468}
1469
Chris Lattner3ce9b672006-09-26 23:59:50 +00001470/// PrintSpecial - Print information related to the specified machine instr
1471/// that is independent of the operand, and may be independent of the instr
1472/// itself. This can be useful for portably encoding the comment character
1473/// or other bits of target-specific knowledge into the asmstrings. The
1474/// syntax used is ${:comment}. Targets can override this to add support
1475/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001476void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001477 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001478 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001479 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001480 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001481 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001482 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001483 // Comparing the address of MI isn't sufficient, because machineinstrs may
1484 // be allocated to the same address across functions.
1485 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1486
Owen Andersonbd58edf2009-06-24 22:28:12 +00001487 // If this is a new LastFn instruction, bump the counter.
1488 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001489 ++Counter;
1490 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001491 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001492 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001493 O << Counter;
1494 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001495 std::string msg;
1496 raw_string_ostream Msg(msg);
1497 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001498 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001499 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001500 }
1501}
1502
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001503/// processDebugLoc - Processes the debug information of each machine
1504/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001505void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1506 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001507 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1508 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001509 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001510 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001511 if (DL.isUnknown())
1512 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001513 DILocation CurDLT = MF->getDILocation(DL);
1514 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001515 return;
1516
1517 if (BeforePrintingInsn) {
Devang Patel6b61f582010-01-16 06:09:35 +00001518 if (CurDLT.getNode() != PrevDLT.getNode()) {
1519 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1520 CurDLT.getColumnNumber(),
1521 CurDLT.getScope().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001522 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001523 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001524 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001525 PrevDLT = CurDLT;
1526 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001527 } else {
1528 // After printing instruction
1529 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001530 }
1531}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001532
Devang Patel53bb5c92009-11-10 23:06:00 +00001533
Chris Lattner0264d1a2006-01-27 02:10:10 +00001534/// printInlineAsm - This method formats and prints the specified machine
1535/// instruction that is an inline asm.
1536void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001537 unsigned NumOperands = MI->getNumOperands();
1538
1539 // Count the number of register definitions.
1540 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001541 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001542 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001543 assert(NumDefs != NumOperands-1 && "No asm string?");
1544
Dan Gohmand735b802008-10-03 15:45:36 +00001545 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001546
1547 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001548 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001549
Dan Gohman40c57862009-11-06 00:04:54 +00001550 O << '\t';
1551
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001552 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1553 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001554 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001555 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1556 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001557 return;
1558 }
1559
Chris Lattner33adcfb2009-08-22 21:43:10 +00001560 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001561
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001562 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001563 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001564
Chris Lattner66099132006-02-01 22:41:11 +00001565 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1566 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001567
Chris Lattner66099132006-02-01 22:41:11 +00001568 while (*LastEmitted) {
1569 switch (*LastEmitted) {
1570 default: {
1571 // Not a special case, emit the string section literally.
1572 const char *LiteralEnd = LastEmitted+1;
1573 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001574 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001575 ++LiteralEnd;
1576 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1577 O.write(LastEmitted, LiteralEnd-LastEmitted);
1578 LastEmitted = LiteralEnd;
1579 break;
1580 }
Chris Lattner1c059972006-05-05 21:47:05 +00001581 case '\n':
1582 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001583 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001584 break;
Chris Lattner66099132006-02-01 22:41:11 +00001585 case '$': {
1586 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001587 bool Done = true;
1588
1589 // Handle escapes.
1590 switch (*LastEmitted) {
1591 default: Done = false; break;
1592 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001593 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1594 O << '$';
1595 ++LastEmitted; // Consume second '$' character.
1596 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001597 case '(': // $( -> same as GCC's { character.
1598 ++LastEmitted; // Consume '(' character.
1599 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001600 llvm_report_error("Nested variants found in inline asm string: '"
1601 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001602 }
1603 CurVariant = 0; // We're in the first variant now.
1604 break;
1605 case '|':
1606 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001607 if (CurVariant == -1)
1608 O << '|'; // this is gcc's behavior for | outside a variant
1609 else
1610 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001611 break;
1612 case ')': // $) -> same as GCC's } char.
1613 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001614 if (CurVariant == -1)
1615 O << '}'; // this is gcc's behavior for } outside a variant
1616 else
1617 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001618 break;
Chris Lattner66099132006-02-01 22:41:11 +00001619 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001620 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001621
1622 bool HasCurlyBraces = false;
1623 if (*LastEmitted == '{') { // ${variable}
1624 ++LastEmitted; // Consume '{' character.
1625 HasCurlyBraces = true;
1626 }
1627
Chris Lattner3e0cc262009-03-10 05:37:13 +00001628 // If we have ${:foo}, then this is not a real operand reference, it is a
1629 // "magic" string reference, just like in .td files. Arrange to call
1630 // PrintSpecial.
1631 if (HasCurlyBraces && *LastEmitted == ':') {
1632 ++LastEmitted;
1633 const char *StrStart = LastEmitted;
1634 const char *StrEnd = strchr(StrStart, '}');
1635 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001636 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1637 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001638 }
1639
1640 std::string Val(StrStart, StrEnd);
1641 PrintSpecial(MI, Val.c_str());
1642 LastEmitted = StrEnd+1;
1643 break;
1644 }
1645
Chris Lattner66099132006-02-01 22:41:11 +00001646 const char *IDStart = LastEmitted;
1647 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001648 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001649 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1650 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001651 llvm_report_error("Bad $ operand number in inline asm string: '"
1652 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001653 }
1654 LastEmitted = IDEnd;
1655
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001656 char Modifier[2] = { 0, 0 };
1657
Chris Lattner66099132006-02-01 22:41:11 +00001658 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001659 // If we have curly braces, check for a modifier character. This
1660 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1661 if (*LastEmitted == ':') {
1662 ++LastEmitted; // Consume ':' character.
1663 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001664 llvm_report_error("Bad ${:} expression in inline asm string: '"
1665 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001666 }
1667
1668 Modifier[0] = *LastEmitted;
1669 ++LastEmitted; // Consume modifier character.
1670 }
1671
Chris Lattner66099132006-02-01 22:41:11 +00001672 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001673 llvm_report_error("Bad ${} expression in inline asm string: '"
1674 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001675 }
1676 ++LastEmitted; // Consume '}' character.
1677 }
1678
1679 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001680 llvm_report_error("Invalid $ operand number in inline asm string: '"
1681 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001682 }
1683
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001684 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001685 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001686 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1687 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001688
1689 bool Error = false;
1690
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001691 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001692 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001693 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001694 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001695 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001696 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001697
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001698 if (OpNo >= MI->getNumOperands()) {
1699 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001700 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001701 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001702 ++OpNo; // Skip over the ID number.
1703
Chris Lattner10b318b2010-01-17 21:43:43 +00001704 if (Modifier[0] == 'l') // labels are target independent
1705 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001706 else {
1707 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001708 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001709 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1710 Modifier[0] ? Modifier : 0);
1711 } else {
1712 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1713 Modifier[0] ? Modifier : 0);
1714 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001715 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001716 }
1717 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001718 std::string msg;
1719 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001720 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001721 MI->print(Msg);
1722 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001723 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001724 }
Chris Lattner66099132006-02-01 22:41:11 +00001725 break;
1726 }
Chris Lattner66099132006-02-01 22:41:11 +00001727 }
1728 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001729 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001730}
1731
Evan Chengda47e6e2008-03-15 00:03:38 +00001732/// printImplicitDef - This method prints the specified machine instruction
1733/// that is an implicit def.
1734void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001735 if (!VerboseAsm) return;
1736 O.PadToColumn(MAI->getCommentColumn());
1737 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001738 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001739}
1740
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001741void AsmPrinter::printKill(const MachineInstr *MI) const {
1742 if (!VerboseAsm) return;
1743 O.PadToColumn(MAI->getCommentColumn());
1744 O << MAI->getCommentString() << " kill:";
1745 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1746 const MachineOperand &op = MI->getOperand(n);
1747 assert(op.isReg() && "KILL instruction must have only register operands");
1748 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1749 }
1750}
1751
Jim Laskey1ee29252007-01-26 14:34:52 +00001752/// printLabel - This method prints a local label used by debug and
1753/// exception handling tables.
1754void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001755 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001756}
1757
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001758void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001759 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001760}
1761
Chris Lattner66099132006-02-01 22:41:11 +00001762/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1763/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001764/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001765bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001766 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001767 // Target doesn't support this yet!
1768 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001769}
Chris Lattnerdd260332006-02-24 20:21:58 +00001770
1771bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1772 unsigned AsmVariant,
1773 const char *ExtraCode) {
1774 // Target doesn't support this yet!
1775 return true;
1776}
Nate Begeman37efe672006-04-22 18:53:45 +00001777
Dan Gohman29cbade2009-11-20 23:18:13 +00001778MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1779 const char *Suffix) const {
1780 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001781}
1782
1783MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001784 const BasicBlock *BB,
1785 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001786 assert(BB->hasName() &&
1787 "Address of anonymous basic block not supported yet!");
1788
Dan Gohman568a3be2009-11-05 23:14:35 +00001789 // This code must use the function name itself, and not the function number,
1790 // since it must be possible to generate the label name from within other
1791 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001792 SmallString<60> FnName;
1793 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001794
Chris Lattner2f8cc262010-01-13 07:30:49 +00001795 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001796 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001797 Mang->getNameWithPrefix(NameResult,
1798 StringRef("BA") + Twine((unsigned)FnName.size()) +
1799 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1800 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001801
Chris Lattner48130352010-01-13 06:38:18 +00001802 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001803}
1804
Chris Lattner7cb384d2009-09-12 23:02:08 +00001805MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1806 SmallString<60> Name;
1807 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1808 << getFunctionNumber() << '_' << MBBID;
1809
1810 return OutContext.GetOrCreateSymbol(Name.str());
1811}
1812
Chris Lattner6b04ede2010-01-15 23:18:17 +00001813/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1814/// value.
1815MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1816 SmallString<60> NameStr;
1817 Mang->getNameWithPrefix(NameStr, GV, false);
1818 return OutContext.GetOrCreateSymbol(NameStr.str());
1819}
1820
Chris Lattner7a2ba942010-01-16 18:37:32 +00001821/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001822/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001823/// symbol is forced to have private linkage if ForcePrivate is true.
1824MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1825 StringRef Suffix,
1826 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001827 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001828 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001829 NameStr.append(Suffix.begin(), Suffix.end());
1830 return OutContext.GetOrCreateSymbol(NameStr.str());
1831}
1832
Chris Lattner6b04ede2010-01-15 23:18:17 +00001833/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1834/// ExternalSymbol.
1835MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1836 SmallString<60> NameStr;
1837 Mang->getNameWithPrefix(NameStr, Sym);
1838 return OutContext.GetOrCreateSymbol(NameStr.str());
1839}
1840
Chris Lattner7cb384d2009-09-12 23:02:08 +00001841
Chris Lattner70a54c02009-09-13 18:25:37 +00001842/// EmitBasicBlockStart - This method prints the label for the specified
1843/// MachineBasicBlock, an alignment (if present) and a comment describing
1844/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001845void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001846 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001847 if (unsigned Align = MBB->getAlignment())
1848 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001849
Dan Gohmanb1cac332009-10-30 01:34:35 +00001850 // If the block has its address taken, emit a special label to satisfy
1851 // references to the block. This is done so that we don't need to
1852 // remember the number of this label, and so that we can make
1853 // forward references to labels without knowing what their numbers
1854 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001855 if (MBB->hasAddressTaken()) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001856 O << *GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1857 MBB->getBasicBlock());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001858 O << ':';
1859 if (VerboseAsm) {
1860 O.PadToColumn(MAI->getCommentColumn());
1861 O << MAI->getCommentString() << " Address Taken";
1862 }
1863 O << '\n';
1864 }
1865
Dan Gohmanb1cac332009-10-30 01:34:35 +00001866 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001867 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1868 if (VerboseAsm)
1869 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1870 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +00001871 O << *GetMBBSymbol(MBB->getNumber()) << ':';
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001872 if (!VerboseAsm)
1873 O << '\n';
1874 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001875
Dan Gohmanb1cac332009-10-30 01:34:35 +00001876 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001877 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001878 if (const BasicBlock *BB = MBB->getBasicBlock())
1879 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001880 O.PadToColumn(MAI->getCommentColumn());
1881 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001882 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001883 }
David Greeneb71d1b22009-08-10 16:38:07 +00001884
Chris Lattner70a54c02009-09-13 18:25:37 +00001885 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001886 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001887 }
Nate Begeman37efe672006-04-22 18:53:45 +00001888}
Nate Begeman52a51e382006-08-12 21:29:52 +00001889
Evan Chengcc415862007-11-09 01:32:10 +00001890/// printPICJumpTableSetLabel - This method prints a set label for the
1891/// specified MachineBasicBlock for a jumptable entry.
1892void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1893 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001894 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001895 return;
1896
Chris Lattner33adcfb2009-08-22 21:43:10 +00001897 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001898 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1899 << *GetMBBSymbol(MBB->getNumber())
1900 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001901 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001902}
Evan Chengd6594ae2006-09-12 21:00:35 +00001903
Evan Chengcc415862007-11-09 01:32:10 +00001904void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1905 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001906 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001907 return;
1908
Chris Lattner33adcfb2009-08-22 21:43:10 +00001909 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001910 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001911 << "_set_" << MBB->getNumber() << ','
1912 << *GetMBBSymbol(MBB->getNumber())
1913 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001914 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001915}
1916
Evan Chengd6594ae2006-09-12 21:00:35 +00001917/// printDataDirective - This method prints the asm directive for the
1918/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001919void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001920 const TargetData *TD = TM.getTargetData();
1921 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001922 case Type::FloatTyID: case Type::DoubleTyID:
1923 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1924 assert(0 && "Should have already output floating point constant.");
1925 default:
1926 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001927 case Type::IntegerTyID: {
1928 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1929 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001930 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001931 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001932 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001933 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001934 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001935 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001936 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001937 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001938 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001939 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001940 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001941 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001942 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001943 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001944 case Type::PointerTyID:
1945 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001946 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001947 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001948 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001949 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001950 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001951 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001952 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001953 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001954 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001955 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001956 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001957 }
1958}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001959
Chris Lattner53d4d782010-01-15 23:38:51 +00001960void AsmPrinter::printVisibility(const MCSymbol *Sym,
1961 unsigned Visibility) const {
1962 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001963 if (const char *Directive = MAI->getHiddenDirective())
1964 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001965 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001966 if (const char *Directive = MAI->getProtectedDirective())
1967 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001968 }
1969}
1970
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001971void AsmPrinter::printOffset(int64_t Offset) const {
1972 if (Offset > 0)
1973 O << '+' << Offset;
1974 else if (Offset < 0)
1975 O << Offset;
1976}
1977
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001978GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1979 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001980 return 0;
1981
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001982 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001983 if (GCPI != GCMetadataPrinters.end())
1984 return GCPI->second;
1985
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001986 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001987
1988 for (GCMetadataPrinterRegistry::iterator
1989 I = GCMetadataPrinterRegistry::begin(),
1990 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1991 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001992 GCMetadataPrinter *GMP = I->instantiate();
1993 GMP->S = S;
1994 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1995 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001996 }
1997
Chris Lattner103289e2009-08-23 07:19:13 +00001998 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001999 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00002000}
David Greene014700c2009-07-13 20:25:48 +00002001
2002/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00002003void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00002004 if (!VerboseAsm)
2005 return;
David Greene3ac1ab82009-07-16 22:24:20 +00002006
David Greene1924aab2009-11-13 21:34:57 +00002007 bool Newline = false;
2008
2009 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00002010 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00002011
2012 // Print source line info.
2013 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00002014 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00002015 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00002016 // Omit the directory, because it's likely to be long and uninteresting.
2017 if (!Scope.isNull())
2018 O << Scope.getFilename();
2019 else
2020 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00002021 O << ':' << DLT.getLineNumber();
2022 if (DLT.getColumnNumber() != 0)
2023 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00002024 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00002025 }
David Greene1924aab2009-11-13 21:34:57 +00002026
David Greeneddff9412009-11-16 15:12:23 +00002027 // Check for spills and reloads
2028 int FI;
2029
2030 const MachineFrameInfo *FrameInfo =
2031 MI.getParent()->getParent()->getFrameInfo();
2032
2033 // We assume a single instruction only has a spill or reload, not
2034 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00002035 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00002036 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
2037 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00002038 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00002039 if (Newline) O << '\n';
2040 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00002041 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00002042 Newline = true;
2043 }
2044 }
David Greenef7ea2a52009-12-04 22:46:04 +00002045 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00002046 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
2047 if (Newline) O << '\n';
2048 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00002049 O << MAI->getCommentString() << ' '
2050 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00002051 Newline = true;
2052 }
2053 }
2054 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
2055 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00002056 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00002057 if (Newline) O << '\n';
2058 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00002059 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00002060 Newline = true;
2061 }
2062 }
David Greenef7ea2a52009-12-04 22:46:04 +00002063 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00002064 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
2065 if (Newline) O << '\n';
2066 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00002067 O << MAI->getCommentString() << ' '
2068 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00002069 Newline = true;
2070 }
2071 }
2072
2073 // Check for spill-induced copies
2074 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
2075 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
2076 SrcSubIdx, DstSubIdx)) {
2077 if (MI.getAsmPrinterFlag(ReloadReuse)) {
2078 if (Newline) O << '\n';
2079 O.PadToColumn(MAI->getCommentColumn());
2080 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00002081 }
2082 }
David Greene014700c2009-07-13 20:25:48 +00002083}
2084
David Greenefe37ab32009-08-18 19:22:55 +00002085/// PrintChildLoopComment - Print comments about child loops within
2086/// the loop for this basic block, with nesting.
2087///
2088static void PrintChildLoopComment(formatted_raw_ostream &O,
2089 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00002090 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00002091 int FunctionNumber) {
2092 // Add child loop information
2093 for(MachineLoop::iterator cl = loop->begin(),
2094 clend = loop->end();
2095 cl != clend;
2096 ++cl) {
2097 MachineBasicBlock *Header = (*cl)->getHeader();
2098 assert(Header && "No header for loop");
2099
2100 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002101 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00002102
Chris Lattner33adcfb2009-08-22 21:43:10 +00002103 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002104 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002105 << " Child Loop BB" << FunctionNumber << "_"
2106 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
2107
Chris Lattner33adcfb2009-08-22 21:43:10 +00002108 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00002109 }
2110}
2111
David Greeneb71d1b22009-08-10 16:38:07 +00002112/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00002113void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00002114 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00002115 // Add loop depth information
2116 const MachineLoop *loop = LI->getLoopFor(&MBB);
2117
2118 if (loop) {
2119 // Print a newline after bb# annotation.
2120 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00002121 O.PadToColumn(MAI->getCommentColumn());
2122 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00002123 << '\n';
2124
Chris Lattner33adcfb2009-08-22 21:43:10 +00002125 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002126
2127 MachineBasicBlock *Header = loop->getHeader();
2128 assert(Header && "No header for loop");
2129
2130 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002131 O << MAI->getCommentString() << " Loop Header";
2132 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002133 }
2134 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002135 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002136 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2137 }
2138
2139 if (loop->empty()) {
2140 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002141 O.PadToColumn(MAI->getCommentColumn());
2142 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002143 }
2144
2145 // Add parent loop information
2146 for (const MachineLoop *CurLoop = loop->getParentLoop();
2147 CurLoop;
2148 CurLoop = CurLoop->getParentLoop()) {
2149 MachineBasicBlock *Header = CurLoop->getHeader();
2150 assert(Header && "No header for loop");
2151
2152 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002153 O.PadToColumn(MAI->getCommentColumn());
2154 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002155 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002156 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002157 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2158 }
2159 }
2160 }
2161}