blob: d439fb6e3c1b3fee39f73af04cce1c1871d421e8 [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.
Chris Lattner16582022010-01-20 06:39:07 +000061 OutStreamer(*createAsmStreamer(OutContext, O, *T,
62 TM.getTargetData()->isLittleEndian(), 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000063
Devang Patel6b61f582010-01-16 06:09:35 +000064 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000065 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000066 switch (AsmVerbose) {
67 case cl::BOU_UNSET: VerboseAsm = VDef; break;
68 case cl::BOU_TRUE: VerboseAsm = true; break;
69 case cl::BOU_FALSE: VerboseAsm = false; break;
70 }
71}
Chris Lattnered138932005-12-13 06:32:10 +000072
Gordon Henriksenc317a602008-08-17 12:08:44 +000073AsmPrinter::~AsmPrinter() {
74 for (gcp_iterator I = GCMetadataPrinters.begin(),
75 E = GCMetadataPrinters.end(); I != E; ++I)
76 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000077
78 delete &OutStreamer;
79 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000080}
Chris Lattnered138932005-12-13 06:32:10 +000081
Chris Lattner38c39882009-08-03 19:12:26 +000082TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000083 return TM.getTargetLowering()->getObjFileLowering();
84}
85
Chris Lattnerdabf07c2009-08-18 06:15:16 +000086/// getCurrentSection() - Return the current section we are emitting to.
87const MCSection *AsmPrinter::getCurrentSection() const {
88 return OutStreamer.getCurrentSection();
89}
90
91
Gordon Henriksence224772008-01-07 01:30:38 +000092void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000093 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000094 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000095 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000096 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000097 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000098}
99
Chris Lattnera80ba712004-08-16 23:15:22 +0000100bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000101 // Initialize TargetLoweringObjectFile.
102 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
103 .Initialize(OutContext, TM);
104
Chris Lattnerc0dba722010-01-17 18:22:35 +0000105 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000106
Bob Wilson812209a2009-09-30 22:06:26 +0000107 // Allow the target to emit any magic that it wants at the start of the file.
108 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000109
Chris Lattner33adcfb2009-08-22 21:43:10 +0000110 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000111 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000112 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000113 a function came from. */
114 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
115 }
116
Bob Wilson812209a2009-09-30 22:06:26 +0000117 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
118 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000119 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
120 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000121 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000122
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000123 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000124 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000125 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000126 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000127 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000128
Chris Lattnerb55e0682009-09-24 05:44:53 +0000129 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
130 if (MMI)
131 MMI->AnalyzeModule(M);
132 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000133 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000134 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000135
Chris Lattnera80ba712004-08-16 23:15:22 +0000136 return false;
137}
138
Chris Lattner48d64ba2010-01-19 04:39:15 +0000139/// EmitGlobalVariable - Emit the specified global variable to the .s file.
140void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
141 if (!GV->hasInitializer()) // External globals require no code.
142 return;
143
144 // Check to see if this is a special global used by LLVM, if so, emit it.
145 if (EmitSpecialLLVMGlobal(GV))
146 return;
Chris Lattner74bfe212010-01-19 05:38:33 +0000147
148 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
149 printVisibility(GVSym, GV->getVisibility());
150
151 if (MAI->hasDotTypeDotSizeDirective()) {
152 O << "\t.type\t" << *GVSym;
153 if (MAI->getCommentString()[0] != '@')
154 O << ",@object\n";
155 else
156 O << ",%object\n";
157 }
Chris Lattner48d64ba2010-01-19 04:39:15 +0000158
Chris Lattner74bfe212010-01-19 05:38:33 +0000159 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
160
161 const TargetData *TD = TM.getTargetData();
162 unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType());
163 unsigned AlignLog = TD->getPreferredAlignmentLog(GV);
164
Chris Lattner9744d612010-01-19 05:51:42 +0000165 // Handle common and BSS local symbols (.lcomm).
166 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000167 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
168
Chris Lattner74bfe212010-01-19 05:38:33 +0000169 if (VerboseAsm) {
170 O.PadToColumn(MAI->getCommentColumn());
171 O << MAI->getCommentString() << ' ';
172 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattner4ed54382010-01-19 06:01:04 +0000173 O << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000174 }
Chris Lattner814819f2010-01-19 06:25:51 +0000175
176 // Handle common symbols.
Chris Lattner9744d612010-01-19 05:51:42 +0000177 if (GVKind.isCommon()) {
178 // .comm _foo, 42, 4
Chris Lattner4ed54382010-01-19 06:01:04 +0000179 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner814819f2010-01-19 06:25:51 +0000180 return;
Chris Lattner9744d612010-01-19 05:51:42 +0000181 }
Chris Lattner814819f2010-01-19 06:25:51 +0000182
183 // Handle local BSS symbols.
184 if (MAI->hasMachoZeroFillDirective()) {
185 const MCSection *TheSection =
186 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
187 // .zerofill __DATA, __bss, _foo, 400, 5
188 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
189 return;
190 }
191
192 if (const char *LComm = MAI->getLCOMMDirective()) {
193 // .lcomm _foo, 42
194 O << LComm << *GVSym << ',' << Size;
195 O << '\n';
196 return;
197 }
198
199 // .local _foo
200 O << "\t.local\t" << *GVSym << '\n';
201 // .comm _foo, 42, 4
202 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner74bfe212010-01-19 05:38:33 +0000203 return;
204 }
205
206 const MCSection *TheSection =
207 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
208
209 // Handle the zerofill directive on darwin, which is a special form of BSS
210 // emission.
211 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
212 // .globl _foo
213 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
214 // .zerofill __DATA, __common, _foo, 400, 5
215 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
216 return;
217 }
218
219 OutStreamer.SwitchSection(TheSection);
220
221 // TODO: Factor into an 'emit linkage' thing that is shared with function
222 // bodies.
223 switch (GV->getLinkage()) {
224 case GlobalValue::CommonLinkage:
225 case GlobalValue::LinkOnceAnyLinkage:
226 case GlobalValue::LinkOnceODRLinkage:
227 case GlobalValue::WeakAnyLinkage:
228 case GlobalValue::WeakODRLinkage:
229 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner4c8c6682010-01-19 06:41:24 +0000230 if (MAI->getWeakDefDirective() != 0) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000231 // .globl _foo
232 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
233 // .weak_definition _foo
Chris Lattner4c8c6682010-01-19 06:41:24 +0000234 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::WeakDefinition);
Chris Lattner74bfe212010-01-19 05:38:33 +0000235 } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
236 // .globl _foo
237 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
238 // .linkonce same_size
239 O << LinkOnce;
Chris Lattner4c8c6682010-01-19 06:41:24 +0000240 } else {
241 // .weak _foo
242 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Weak);
243 }
Chris Lattner74bfe212010-01-19 05:38:33 +0000244 break;
245 case GlobalValue::DLLExportLinkage:
246 case GlobalValue::AppendingLinkage:
247 // FIXME: appending linkage variables should go into a section of
248 // their name or something. For now, just emit them as external.
249 case GlobalValue::ExternalLinkage:
250 // If external or appending, declare as a global symbol.
251 // .globl _foo
252 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
253 break;
254 case GlobalValue::PrivateLinkage:
255 case GlobalValue::InternalLinkage:
256 break;
257 default:
258 llvm_unreachable("Unknown linkage type!");
259 }
260
261 EmitAlignment(AlignLog, GV);
Chris Lattner74bfe212010-01-19 05:38:33 +0000262 if (VerboseAsm) {
263 O.PadToColumn(MAI->getCommentColumn());
264 O << MAI->getCommentString() << ' ';
265 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattner4c8c6682010-01-19 06:41:24 +0000266 O << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000267 }
Chris Lattner4c8c6682010-01-19 06:41:24 +0000268 OutStreamer.EmitLabel(GVSym);
Chris Lattner74bfe212010-01-19 05:38:33 +0000269
270 EmitGlobalConstant(GV->getInitializer());
271
272 if (MAI->hasDotTypeDotSizeDirective())
273 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
Chris Lattner48d64ba2010-01-19 04:39:15 +0000274}
275
276
Chris Lattnera80ba712004-08-16 23:15:22 +0000277bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000278 // Emit global variables.
279 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
280 I != E; ++I)
Chris Lattner48d64ba2010-01-19 04:39:15 +0000281 EmitGlobalVariable(I);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000282
Chris Lattner1f522fe2009-06-24 18:54:37 +0000283 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000284 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000285 DW->EndModule();
286
Chris Lattner0a7befa2009-06-24 18:52:01 +0000287 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000288 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000289 // FIXME: This is not lazy, it would be nice to only print weak references
290 // to stuff that is actually used. Note that doing so would require targets
291 // to notice uses in operands (due to constant exprs etc). This should
292 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000293
Chris Lattner0a7befa2009-06-24 18:52:01 +0000294 // Print out module-level global variables here.
295 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
296 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000297 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000298 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000299 }
300
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000301 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000302 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000303 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000304 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000305 }
306
Chris Lattner33adcfb2009-08-22 21:43:10 +0000307 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000308 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000309 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000310 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000311 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000312
313 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000314 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000315
Chris Lattner10b318b2010-01-17 21:43:43 +0000316 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
317 O << "\t.globl\t" << *Name << '\n';
318 else if (I->hasWeakLinkage())
319 O << MAI->getWeakRefDirective() << *Name << '\n';
320 else
Chris Lattner10595492010-01-16 01:37:14 +0000321 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000322
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000323 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000324
Chris Lattner10b318b2010-01-17 21:43:43 +0000325 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000326 }
327 }
328
Duncan Sands1465d612009-01-28 13:14:17 +0000329 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000330 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
331 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
332 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000333 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000334
Dan Gohmana779a982008-05-05 00:28:39 +0000335 // If we don't have any trampolines, then we don't require stack memory
336 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000337 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000338 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000339 if (MAI->getNonexecutableStackDirective())
340 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000341
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000342
343 // Allow the target to emit any magic that it wants at the end of the file,
344 // after everything else has gone out.
345 EmitEndOfAsmFile(M);
346
Chris Lattnera80ba712004-08-16 23:15:22 +0000347 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000348 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000349
350 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000351 return false;
352}
353
Chris Lattner25045bd2005-11-21 07:51:36 +0000354void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000355 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000356 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000357 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000358
Chris Lattner25d812b2009-09-16 00:35:39 +0000359 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000360 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000361}
362
Evan Cheng1606e8e2009-03-13 07:51:59 +0000363namespace {
364 // SectionCPs - Keep track the alignment, constpool entries per Section.
365 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000366 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000367 unsigned Alignment;
368 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000369 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000370 };
371}
372
Chris Lattner3b4fd322005-11-21 08:25:09 +0000373/// EmitConstantPool - Print to the current output stream assembly
374/// representations of the constants in the constant pool MCP. This is
375/// used to print out constants which have been "spilled to memory" by
376/// the code generator.
377///
378void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000379 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000380 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000381
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000382 // Calculate sections for constant pool entries. We collect entries to go into
383 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000384 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000385 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000386 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000387 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000388
389 SectionKind Kind;
390 switch (CPE.getRelocationInfo()) {
391 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000392 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000393 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000394 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000395 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000396 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000397 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000398 case 4: Kind = SectionKind::getMergeableConst4(); break;
399 case 8: Kind = SectionKind::getMergeableConst8(); break;
400 case 16: Kind = SectionKind::getMergeableConst16();break;
401 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000402 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000403 }
404
Chris Lattner83d77fa2009-08-01 23:46:12 +0000405 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000406
Evan Cheng1606e8e2009-03-13 07:51:59 +0000407 // The number of sections are small, just do a linear search from the
408 // last section to the first.
409 bool Found = false;
410 unsigned SecIdx = CPSections.size();
411 while (SecIdx != 0) {
412 if (CPSections[--SecIdx].S == S) {
413 Found = true;
414 break;
415 }
416 }
417 if (!Found) {
418 SecIdx = CPSections.size();
419 CPSections.push_back(SectionCPs(S, Align));
420 }
421
422 if (Align > CPSections[SecIdx].Alignment)
423 CPSections[SecIdx].Alignment = Align;
424 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000425 }
426
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000427 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000428 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000429 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000430 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000431
Evan Cheng1606e8e2009-03-13 07:51:59 +0000432 unsigned Offset = 0;
433 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
434 unsigned CPI = CPSections[i].CPEs[j];
435 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000436
Chris Lattner3029f922006-02-09 04:46:04 +0000437 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000438 unsigned AlignMask = CPE.getAlignment() - 1;
439 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Chris Lattneraaec2052010-01-19 19:46:13 +0000440 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000441
442 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000443 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000444
Chris Lattner33adcfb2009-08-22 21:43:10 +0000445 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000446 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000447 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000448 O.PadToColumn(MAI->getCommentColumn());
449 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000450 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000451 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000452 O << '\n';
453 if (CPE.isMachineConstantPoolEntry())
454 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
455 else
456 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000457 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000458 }
459}
460
Nate Begeman37efe672006-04-22 18:53:45 +0000461/// EmitJumpTableInfo - Print assembly representations of the jump tables used
462/// by the current function to the current output stream.
463///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000464void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
465 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000466 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
467 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000468
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000469 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000470
Nate Begeman2f1ae882006-07-27 01:13:04 +0000471 // Pick the directive to use to print the jump table entries, and switch to
472 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000473 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000474
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000475 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000476 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000477 if (F->isWeakForLinker() ||
478 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000479 // In PIC mode, we need to emit the jump table to the same section as the
480 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000481 // We should also do if the section name is NULL or function is declared in
482 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000483 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
484 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000485 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000486 // Otherwise, drop it in the readonly section.
487 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000488 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000489 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000490 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000491 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000492
493 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000494
Nate Begeman37efe672006-04-22 18:53:45 +0000495 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000496 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000497
498 // If this jump table was deleted, ignore it.
499 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000500
501 // For PIC codegen, if possible we want to use the SetDirective to reduce
502 // the number of relocations the assembler will generate for the jump table.
503 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000504 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000505 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000506 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000507 if (EmittedSets.insert(JTBBs[ii]))
508 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000509
Chris Lattner7c301912009-09-13 19:02:16 +0000510 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000511 // before each jump table. The first label is never referenced, but tells
512 // the assembler and linker the extents of the jump table object. The
513 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000514 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
515 O << MAI->getLinkerPrivateGlobalPrefix()
516 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000517 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000518
Chris Lattner33adcfb2009-08-22 21:43:10 +0000519 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000520 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000521
Nate Begeman37efe672006-04-22 18:53:45 +0000522 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000523 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000524 O << '\n';
525 }
526 }
527}
528
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000529void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
530 const MachineBasicBlock *MBB,
531 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000532 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000533
534 // Use JumpTableDirective otherwise honor the entry size from the jump table
535 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000536 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000537 bool HadJTEntryDirective = JTEntryDirective != NULL;
538 if (!HadJTEntryDirective) {
539 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000540 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000541 }
542
543 O << JTEntryDirective << ' ';
544
545 // If we have emitted set directives for the jump table entries, print
546 // them rather than the entries themselves. If we're emitting PIC, then
547 // emit the table entries as differences between two text section labels.
548 // If we're emitting non-PIC code, then emit the entries as direct
549 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000550 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000551 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000552 } else if (MAI->getSetDirective()) {
553 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000554 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000555 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000556 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000557 // If the arch uses custom Jump Table directives, don't calc relative to
558 // JT
559 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000560 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000561 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000562 }
563}
564
565
Chris Lattnered138932005-12-13 06:32:10 +0000566/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
567/// special global used by LLVM. If so, emit it and return true, otherwise
568/// do nothing and return false.
569bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000570 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000571 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000572 EmitLLVMUsedList(GV->getInitializer());
573 return true;
574 }
575
Chris Lattner401e10c2009-07-20 06:14:25 +0000576 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000577 if (GV->getSection() == "llvm.metadata" ||
578 GV->hasAvailableExternallyLinkage())
579 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000580
581 if (!GV->hasAppendingLinkage()) return false;
582
583 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000584
Evan Cheng916d07c2007-06-04 20:39:18 +0000585 const TargetData *TD = TM.getTargetData();
586 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000587 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000588 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000589 EmitAlignment(Align, 0);
590 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000591
592 if (TM.getRelocationModel() == Reloc::Static &&
593 MAI->hasStaticCtorDtorReferenceInStaticMode())
594 O << ".reference .constructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000595 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000596 }
597
Chris Lattnerf231c072009-03-09 05:52:15 +0000598 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000599 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000600 EmitAlignment(Align, 0);
601 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000602
603 if (TM.getRelocationModel() == Reloc::Static &&
604 MAI->hasStaticCtorDtorReferenceInStaticMode())
605 O << ".reference .destructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000606 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000607 }
608
609 return false;
610}
611
Chris Lattner33adcfb2009-08-22 21:43:10 +0000612/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000613/// global in the specified llvm.used list for which emitUsedDirectiveFor
614/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000615void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000616 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000617
Dan Gohmana119de82009-06-14 23:30:43 +0000618 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000619 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
620 if (InitList == 0) return;
621
622 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000623 const GlobalValue *GV =
624 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000625 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000626 O << Directive;
627 EmitConstantValueOnly(InitList->getOperand(i));
628 O << '\n';
629 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000630 }
631}
632
Chris Lattnered138932005-12-13 06:32:10 +0000633/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
634/// function pointers, ignoring the init priority.
635void AsmPrinter::EmitXXStructorList(Constant *List) {
636 // Should be an array of '{ int, void ()* }' structs. The first value is the
637 // init priority, which we ignore.
638 if (!isa<ConstantArray>(List)) return;
639 ConstantArray *InitList = cast<ConstantArray>(List);
640 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
641 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
642 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000643
644 if (CS->getOperand(1)->isNullValue())
645 return; // Found a null terminator, exit printing.
646 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000647 EmitGlobalConstant(CS->getOperand(1));
648 }
649}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000650
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000651
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000652//===----------------------------------------------------------------------===//
653/// LEB 128 number encoding.
654
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000655/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000656/// representing an unsigned leb128 value.
657void AsmPrinter::PrintULEB128(unsigned Value) const {
658 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000659 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000660 Value >>= 7;
661 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000662 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000663 if (Value) O << ", ";
664 } while (Value);
665}
666
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000667/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000668/// representing a signed leb128 value.
669void AsmPrinter::PrintSLEB128(int Value) const {
670 int Sign = Value >> (8 * sizeof(Value) - 1);
671 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000672
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000673 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000674 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000675 Value >>= 7;
676 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
677 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000678 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000679 if (IsMore) O << ", ";
680 } while (IsMore);
681}
682
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000683//===--------------------------------------------------------------------===//
684// Emission and print routines
685//
686
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000687/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000688///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000689void AsmPrinter::PrintHex(uint64_t Value) const {
690 O << "0x";
691 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000692}
693
694/// EOL - Print a newline character to asm stream. If a comment is present
695/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000696void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000697 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000698}
Owen Anderson25995092008-07-01 21:16:27 +0000699
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000700void AsmPrinter::EOL(const Twine &Comment) const {
701 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000702 O.PadToColumn(MAI->getCommentColumn());
703 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000704 << ' '
705 << Comment;
706 }
707 O << '\n';
708}
709
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000710static const char *DecodeDWARFEncoding(unsigned Encoding) {
711 switch (Encoding) {
712 case dwarf::DW_EH_PE_absptr:
713 return "absptr";
714 case dwarf::DW_EH_PE_omit:
715 return "omit";
716 case dwarf::DW_EH_PE_pcrel:
717 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000718 case dwarf::DW_EH_PE_udata4:
719 return "udata4";
720 case dwarf::DW_EH_PE_udata8:
721 return "udata8";
722 case dwarf::DW_EH_PE_sdata4:
723 return "sdata4";
724 case dwarf::DW_EH_PE_sdata8:
725 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000726 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
727 return "pcrel udata4";
728 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
729 return "pcrel sdata4";
730 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
731 return "pcrel udata8";
732 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
733 return "pcrel sdata8";
734 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
735 return "indirect pcrel udata4";
736 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
737 return "indirect pcrel sdata4";
738 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
739 return "indirect pcrel udata8";
740 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
741 return "indirect pcrel sdata8";
742 }
743
744 return 0;
745}
746
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000747void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
748 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000749 O.PadToColumn(MAI->getCommentColumn());
750 O << MAI->getCommentString()
751 << ' '
752 << Comment;
753
754 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
755 O << " (" << EncStr << ')';
756 }
757 O << '\n';
758}
759
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000760/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
761/// unsigned leb128 value.
762void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000764 O << "\t.uleb128\t"
765 << Value;
766 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000767 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000768 PrintULEB128(Value);
769 }
770}
771
772/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
773/// signed leb128 value.
774void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000775 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000776 O << "\t.sleb128\t"
777 << Value;
778 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000779 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000780 PrintSLEB128(Value);
781 }
782}
783
784/// EmitInt8 - Emit a byte directive and value.
785///
786void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000787 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000788 PrintHex(Value & 0xFF);
789}
790
791/// EmitInt16 - Emit a short directive and value.
792///
793void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000794 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000795 PrintHex(Value & 0xFFFF);
796}
797
798/// EmitInt32 - Emit a long directive and value.
799///
800void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000801 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000802 PrintHex(Value);
803}
804
805/// EmitInt64 - Emit a long long directive and value.
806///
807void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000808 if (MAI->getData64bitsDirective()) {
809 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000810 PrintHex(Value);
811 } else {
812 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000813 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000814 EmitInt32(unsigned(Value));
815 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000816 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000817 EmitInt32(unsigned(Value >> 32));
818 }
819 }
820}
821
822/// toOctal - Convert the low order bits of X into an octal digit.
823///
824static inline char toOctal(int X) {
825 return (X&7)+'0';
826}
827
828/// printStringChar - Print a char, escaped if necessary.
829///
David Greene71847812009-07-14 20:18:05 +0000830static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000831 if (C == '"') {
832 O << "\\\"";
833 } else if (C == '\\') {
834 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000835 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000836 O << C;
837 } else {
838 switch(C) {
839 case '\b': O << "\\b"; break;
840 case '\f': O << "\\f"; break;
841 case '\n': O << "\\n"; break;
842 case '\r': O << "\\r"; break;
843 case '\t': O << "\\t"; break;
844 default:
845 O << '\\';
846 O << toOctal(C >> 6);
847 O << toOctal(C >> 3);
848 O << toOctal(C >> 0);
849 break;
850 }
851 }
852}
853
854/// EmitString - Emit a string with quotes and a null terminator.
855/// Special characters are emitted properly.
856/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000857void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000858 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000859}
860
861void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000862 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000863 if (AscizDirective)
864 O << AscizDirective;
865 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000866 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000867 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000868 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000869 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000870 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000871 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000872 else
873 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000874}
875
876
Dan Gohman189f80d2007-09-24 20:58:13 +0000877/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000878void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000879 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000880 for (unsigned i = 0, N = Name.size(); i < N; ++i)
881 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000882 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000883}
884
885
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000886//===----------------------------------------------------------------------===//
887
Chris Lattner3a420532007-05-31 18:57:45 +0000888// EmitAlignment - Emit an alignment directive to the specified power of
889// two boundary. For example, if you pass in 3 here, you will get an 8
890// byte alignment. If a global value is specified, and if that global has
891// an explicit alignment requested, it will unconditionally override the
892// alignment request. However, if ForcedAlignBits is specified, this value
893// has final say: the ultimate alignment will be the max of ForcedAlignBits
894// and the alignment computed with NumBits and the global.
895//
896// The algorithm is:
897// Align = NumBits;
898// if (GV && GV->hasalignment) Align = GV->getalignment();
899// Align = std::max(Align, ForcedAlignBits);
900//
901void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000902 unsigned ForcedAlignBits,
903 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000904 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000905 NumBits = Log2_32(GV->getAlignment());
906 NumBits = std::max(NumBits, ForcedAlignBits);
907
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000908 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000909
910 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000911 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000912 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000913
914 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000915}
David Greenea5bb59f2009-08-05 21:00:52 +0000916
Chris Lattnera80ba712004-08-16 23:15:22 +0000917// Print out the specified constant, without a storage class. Only the
918// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000919void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000920 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000921 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000922 return;
923 }
924
925 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000926 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000927 return;
928 }
929
930 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000931 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000932 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000933 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000934 return;
935 }
936
937 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000938 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000939 return;
940 }
941
942 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
943 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000944 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000945 O << '0';
946 return;
947 }
948
949 switch (CE->getOpcode()) {
950 case Instruction::ZExt:
951 case Instruction::SExt:
952 case Instruction::FPTrunc:
953 case Instruction::FPExt:
954 case Instruction::UIToFP:
955 case Instruction::SIToFP:
956 case Instruction::FPToUI:
957 case Instruction::FPToSI:
958 default:
959 llvm_unreachable("FIXME: Don't support this constant cast expr");
960 case Instruction::GetElementPtr: {
961 // generate a symbolic expression for the byte address
962 const TargetData *TD = TM.getTargetData();
963 const Constant *ptrVal = CE->getOperand(0);
964 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
965 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
966 idxVec.size());
967 if (Offset == 0)
968 return EmitConstantValueOnly(ptrVal);
969
970 // Truncate/sext the offset to the pointer size.
971 if (TD->getPointerSizeInBits() != 64) {
972 int SExtAmount = 64-TD->getPointerSizeInBits();
973 Offset = (Offset << SExtAmount) >> SExtAmount;
974 }
975
976 if (Offset)
977 O << '(';
978 EmitConstantValueOnly(ptrVal);
979 if (Offset > 0)
980 O << ") + " << Offset;
981 else
982 O << ") - " << -Offset;
983 return;
984 }
985 case Instruction::BitCast:
986 return EmitConstantValueOnly(CE->getOperand(0));
987
988 case Instruction::IntToPtr: {
989 // Handle casts to pointers by changing them into casts to the appropriate
990 // integer type. This promotes constant folding and simplifies this code.
991 const TargetData *TD = TM.getTargetData();
992 Constant *Op = CE->getOperand(0);
993 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
994 false/*ZExt*/);
995 return EmitConstantValueOnly(Op);
996 }
997
998 case Instruction::PtrToInt: {
999 // Support only foldable casts to/from pointers that can be eliminated by
1000 // changing the pointer to the appropriately sized integer type.
1001 Constant *Op = CE->getOperand(0);
1002 const Type *Ty = CE->getType();
1003 const TargetData *TD = TM.getTargetData();
1004
1005 // We can emit the pointer value into this slot if the slot is an
1006 // integer slot greater or equal to the size of the pointer.
1007 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
1008 return EmitConstantValueOnly(Op);
1009
1010 O << "((";
1011 EmitConstantValueOnly(Op);
1012 APInt ptrMask =
1013 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
1014
1015 SmallString<40> S;
1016 ptrMask.toStringUnsigned(S);
1017 O << ") & " << S.str() << ')';
1018 return;
1019 }
1020
1021 case Instruction::Trunc:
1022 // We emit the value and depend on the assembler to truncate the generated
1023 // expression properly. This is important for differences between
1024 // blockaddress labels. Since the two labels are in the same function, it
1025 // is reasonable to treat their delta as a 32-bit value.
1026 return EmitConstantValueOnly(CE->getOperand(0));
1027
1028 case Instruction::Add:
1029 case Instruction::Sub:
1030 case Instruction::And:
1031 case Instruction::Or:
1032 case Instruction::Xor:
1033 O << '(';
1034 EmitConstantValueOnly(CE->getOperand(0));
1035 O << ')';
1036 switch (CE->getOpcode()) {
1037 case Instruction::Add:
1038 O << " + ";
1039 break;
1040 case Instruction::Sub:
1041 O << " - ";
1042 break;
1043 case Instruction::And:
1044 O << " & ";
1045 break;
1046 case Instruction::Or:
1047 O << " | ";
1048 break;
1049 case Instruction::Xor:
1050 O << " ^ ";
1051 break;
1052 default:
1053 break;
1054 }
1055 O << '(';
1056 EmitConstantValueOnly(CE->getOperand(1));
1057 O << ')';
1058 break;
Chris Lattnera80ba712004-08-16 23:15:22 +00001059 }
1060}
Chris Lattner1b7e2352004-08-17 06:36:49 +00001061
Chris Lattner2980cef2005-11-10 18:06:33 +00001062/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +00001063/// the predicate isString is true.
1064///
David Greene71847812009-07-14 20:18:05 +00001065static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +00001066 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +00001067 assert(CVA->isString() && "Array is not string compatible!");
1068
Dan Gohmand19a53b2008-06-30 22:03:41 +00001069 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +00001070 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001071 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +00001072 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001073 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001074 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001075 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001076}
1077
Jeff Cohenc884db42006-05-02 01:16:28 +00001078/// EmitString - Emit a zero-byte-terminated string constant.
1079///
1080void AsmPrinter::EmitString(const ConstantArray *CVA) const {
1081 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001082 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +00001083 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001084 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001085 printAsCString(O, CVA, NumElts-1);
1086 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001087 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001088 printAsCString(O, CVA, NumElts);
1089 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001090 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +00001091}
1092
Chris Lattner91093ec2010-01-19 19:10:44 +00001093static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
1094 AsmPrinter &AP) {
1095 if (AddrSpace == 0 && CA->isString()) {
1096 AP.EmitString(CA);
Dan Gohman00d448a2008-12-22 21:14:27 +00001097 } else { // Not a string. Print the values in successive locations
Chris Lattner91093ec2010-01-19 19:10:44 +00001098 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1099 AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001100 }
1101}
1102
Chris Lattner91093ec2010-01-19 19:10:44 +00001103static void EmitGlobalConstantVector(const ConstantVector *CV,
1104 unsigned AddrSpace, AsmPrinter &AP) {
1105 const VectorType *VTy = CV->getType();
1106 for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i)
1107 AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001108}
1109
Chris Lattner91093ec2010-01-19 19:10:44 +00001110static void EmitGlobalConstantStruct(const ConstantStruct *CS,
1111 unsigned AddrSpace, AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001112 // Print the fields in successive locations. Pad to align if needed!
Chris Lattner91093ec2010-01-19 19:10:44 +00001113 const TargetData *TD = AP.TM.getTargetData();
1114 unsigned Size = TD->getTypeAllocSize(CS->getType());
1115 const StructLayout *cvsLayout = TD->getStructLayout(CS->getType());
1116 uint64_t SizeSoFar = 0;
1117 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
1118 const Constant *field = CS->getOperand(i);
Dan Gohman00d448a2008-12-22 21:14:27 +00001119
1120 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001121 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001122 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1123 - cvsLayout->getElementOffset(i)) - fieldSize;
Chris Lattner91093ec2010-01-19 19:10:44 +00001124 SizeSoFar += fieldSize + padSize;
Dan Gohman00d448a2008-12-22 21:14:27 +00001125
1126 // Now print the actual field value.
Chris Lattner91093ec2010-01-19 19:10:44 +00001127 AP.EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001128
1129 // Insert padding - this may include padding to increase the size of the
1130 // current field up to the ABI size (if the struct is not packed) as well
1131 // as padding to ensure that the next field starts at the right offset.
Chris Lattner6449abf2010-01-19 21:51:22 +00001132 AP.OutStreamer.EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001133 }
Chris Lattner91093ec2010-01-19 19:10:44 +00001134 assert(SizeSoFar == cvsLayout->getSizeInBytes() &&
Dan Gohman00d448a2008-12-22 21:14:27 +00001135 "Layout of constant struct may be incorrect!");
1136}
1137
Chris Lattner91093ec2010-01-19 19:10:44 +00001138void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001139 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001140 // FP Constants are printed as integer constants to avoid losing
1141 // precision...
Chris Lattner91093ec2010-01-19 19:10:44 +00001142 const TargetData &TD = *TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001143 if (CFP->getType()->isDoubleTy()) {
Chris Lattner09ce6742010-01-19 22:16:33 +00001144 if (VerboseAsm) {
1145 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1146 O.PadToColumn(MAI->getCommentColumn());
1147 O << MAI->getCommentString() << " double " << Val << '\n';
1148 }
1149
Dan Gohman00d448a2008-12-22 21:14:27 +00001150 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001151 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner09ce6742010-01-19 22:16:33 +00001152 O << MAI->getData64bitsDirective(AddrSpace) << i << '\n';
Chris Lattner91093ec2010-01-19 19:10:44 +00001153 } else if (TD.isBigEndian()) {
Chris Lattner09ce6742010-01-19 22:16:33 +00001154 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32) << '\n';
1155 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i) << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001156 } else {
Chris Lattner09ce6742010-01-19 22:16:33 +00001157 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i) << '\n';
1158 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32) << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001159 }
1160 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001161 }
1162
1163 if (CFP->getType()->isFloatTy()) {
Dan Gohmana12e9d72009-08-12 18:32:22 +00001164 if (VerboseAsm) {
Chris Lattner09ce6742010-01-19 22:16:33 +00001165 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001166 O.PadToColumn(MAI->getCommentColumn());
Chris Lattner09ce6742010-01-19 22:16:33 +00001167 O << MAI->getCommentString() << " float " << Val << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 }
Chris Lattner09ce6742010-01-19 22:16:33 +00001169 O << MAI->getData32bitsDirective(AddrSpace)
1170 << CFP->getValueAPF().bitcastToAPInt().getZExtValue() << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001171 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001172 }
1173
1174 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001175 // all long double variants are printed as hex
1176 // api needed to prevent premature destruction
Chris Lattner09ce6742010-01-19 22:16:33 +00001177 APInt API = CFP->getValueAPF().bitcastToAPInt();
1178 const uint64_t *p = API.getRawData();
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001179 if (VerboseAsm) {
1180 // Convert to double so we can print the approximate val as a comment.
1181 APFloat DoubleVal = CFP->getValueAPF();
1182 bool ignored;
1183 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1184 &ignored);
1185 O.PadToColumn(MAI->getCommentColumn());
1186 O << MAI->getCommentString() << " x86_fp80 ~= "
1187 << DoubleVal.convertToDouble() << '\n';
1188 }
1189
Chris Lattner91093ec2010-01-19 19:10:44 +00001190 if (TD.isBigEndian()) {
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001191 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]) << '\n';
1192 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48)<<'\n';
1193 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32)<<'\n';
1194 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16)<<'\n';
1195 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]) <<'\n';
1196 } else {
1197 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]) << '\n';
1198 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16)<<'\n';
1199 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32)<<'\n';
1200 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48)<<'\n';
1201 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]) << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001202 }
Chris Lattner6449abf2010-01-19 21:51:22 +00001203 OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) -
1204 TD.getTypeStoreSize(CFP->getType()), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001205 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001206 }
1207
Chris Lattner09ce6742010-01-19 22:16:33 +00001208 assert(CFP->getType()->isPPC_FP128Ty() &&
1209 "Floating point constant type not handled");
1210 // All long double variants are printed as hex api needed to prevent
1211 // premature destruction.
1212 APInt API = CFP->getValueAPF().bitcastToAPInt();
1213 const uint64_t *p = API.getRawData();
1214 if (TD.isBigEndian()) {
1215 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32)<<'\n';
1216 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0])<<'\n';
1217 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32)<<'\n';
1218 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1])<<'\n';
1219 } else {
1220 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1])<<'\n';
1221 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32)<<'\n';
1222 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0])<<'\n';
1223 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32)<<'\n';
1224 }
Dan Gohman00d448a2008-12-22 21:14:27 +00001225}
1226
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001227void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1228 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001229 const TargetData *TD = TM.getTargetData();
1230 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001231 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001232
1233 // We don't expect assemblers to support integer data directives
1234 // for more than 64 bits, so we emit the data in at most 64-bit
1235 // quantities at a time.
1236 const uint64_t *RawData = CI->getValue().getRawData();
1237 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1238 uint64_t Val;
1239 if (TD->isBigEndian())
1240 Val = RawData[e - i - 1];
1241 else
1242 Val = RawData[i];
1243
Chris Lattner5979dff2010-01-13 04:38:16 +00001244 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001245 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001246 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001247 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001248
1249 // Emit two 32-bit chunks, order depends on endianness.
1250 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1251 const char *FirstName = " least", *SecondName = " most";
1252 if (TD->isBigEndian()) {
1253 std::swap(FirstChunk, SecondChunk);
1254 std::swap(FirstName, SecondName);
1255 }
1256
1257 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1258 if (VerboseAsm) {
1259 O.PadToColumn(MAI->getCommentColumn());
1260 O << MAI->getCommentString()
1261 << FirstName << " significant half of i64 " << Val;
1262 }
1263 O << '\n';
1264
1265 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1266 if (VerboseAsm) {
1267 O.PadToColumn(MAI->getCommentColumn());
1268 O << MAI->getCommentString()
1269 << SecondName << " significant half of i64 " << Val;
1270 }
1271 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001272 }
1273}
1274
Chris Lattner25045bd2005-11-21 07:51:36 +00001275/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001276void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001277 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001278 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001279 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001280
Chris Lattner8a6d7ac2010-01-19 18:58:52 +00001281 if (CV->isNullValue() || isa<UndefValue>(CV))
Chris Lattner6449abf2010-01-19 21:51:22 +00001282 return OutStreamer.EmitZeros(Size, AddrSpace);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001283
Chris Lattner91093ec2010-01-19 19:10:44 +00001284 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
1285 return EmitGlobalConstantArray(CVA, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001286
Chris Lattner91093ec2010-01-19 19:10:44 +00001287 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
1288 return EmitGlobalConstantStruct(CVS, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001289
Chris Lattner91093ec2010-01-19 19:10:44 +00001290 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
1291 return EmitGlobalConstantFP(CFP, AddrSpace);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001292
1293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1294 // If we can directly emit an 8-byte constant, do it.
1295 if (Size == 8)
1296 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1297 O << Data64Dir << CI->getZExtValue() << '\n';
1298 return;
1299 }
1300
Dan Gohman00d448a2008-12-22 21:14:27 +00001301 // Small integers are handled below; large integers are handled here.
1302 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001303 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001304 return;
1305 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001306 }
1307
Chris Lattner91093ec2010-01-19 19:10:44 +00001308 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1309 return EmitGlobalConstantVector(V, AddrSpace, *this);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001310
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001311 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001312 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001313 if (VerboseAsm) {
1314 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1315 SmallString<40> S;
1316 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001317 O.PadToColumn(MAI->getCommentColumn());
1318 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001319 }
Scott Micheleefc8452008-06-03 15:39:51 +00001320 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001321 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001322}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001323
Chris Lattnerfad86b02008-08-17 07:19:36 +00001324void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001325 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001326 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001327}
1328
Chris Lattner3ce9b672006-09-26 23:59:50 +00001329/// PrintSpecial - Print information related to the specified machine instr
1330/// that is independent of the operand, and may be independent of the instr
1331/// itself. This can be useful for portably encoding the comment character
1332/// or other bits of target-specific knowledge into the asmstrings. The
1333/// syntax used is ${:comment}. Targets can override this to add support
1334/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001335void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001336 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001337 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001338 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001339 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001340 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001341 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001342 // Comparing the address of MI isn't sufficient, because machineinstrs may
1343 // be allocated to the same address across functions.
1344 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1345
Owen Andersonbd58edf2009-06-24 22:28:12 +00001346 // If this is a new LastFn instruction, bump the counter.
1347 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001348 ++Counter;
1349 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001350 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001351 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001352 O << Counter;
1353 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001354 std::string msg;
1355 raw_string_ostream Msg(msg);
1356 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001357 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001358 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001359 }
1360}
1361
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001362/// processDebugLoc - Processes the debug information of each machine
1363/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001364void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1365 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001366 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1367 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001368 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001369 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001370 if (DL.isUnknown())
1371 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001372 DILocation CurDLT = MF->getDILocation(DL);
1373 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001374 return;
1375
1376 if (BeforePrintingInsn) {
Devang Patelc99fd872010-01-19 06:09:04 +00001377 if (CurDLT.getNode() != PrevDLT) {
Devang Patel6b61f582010-01-16 06:09:35 +00001378 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1379 CurDLT.getColumnNumber(),
1380 CurDLT.getScope().getNode());
Devang Patel53bb5c92009-11-10 23:06:00 +00001381 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001382 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001383 DW->BeginScope(MI, L);
Devang Patelc99fd872010-01-19 06:09:04 +00001384 PrevDLT = CurDLT.getNode();
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001385 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001386 } else {
1387 // After printing instruction
1388 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001389 }
1390}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001391
Devang Patel53bb5c92009-11-10 23:06:00 +00001392
Chris Lattner0264d1a2006-01-27 02:10:10 +00001393/// printInlineAsm - This method formats and prints the specified machine
1394/// instruction that is an inline asm.
1395void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001396 unsigned NumOperands = MI->getNumOperands();
1397
1398 // Count the number of register definitions.
1399 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001400 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001401 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001402 assert(NumDefs != NumOperands-1 && "No asm string?");
1403
Dan Gohmand735b802008-10-03 15:45:36 +00001404 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001405
1406 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001407 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001408
Dan Gohman40c57862009-11-06 00:04:54 +00001409 O << '\t';
1410
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001411 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1412 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001413 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001414 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1415 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001416 return;
1417 }
1418
Chris Lattner33adcfb2009-08-22 21:43:10 +00001419 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001420
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001421 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001422 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001423
Chris Lattner66099132006-02-01 22:41:11 +00001424 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1425 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001426
Chris Lattner66099132006-02-01 22:41:11 +00001427 while (*LastEmitted) {
1428 switch (*LastEmitted) {
1429 default: {
1430 // Not a special case, emit the string section literally.
1431 const char *LiteralEnd = LastEmitted+1;
1432 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001433 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001434 ++LiteralEnd;
1435 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1436 O.write(LastEmitted, LiteralEnd-LastEmitted);
1437 LastEmitted = LiteralEnd;
1438 break;
1439 }
Chris Lattner1c059972006-05-05 21:47:05 +00001440 case '\n':
1441 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001442 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001443 break;
Chris Lattner66099132006-02-01 22:41:11 +00001444 case '$': {
1445 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001446 bool Done = true;
1447
1448 // Handle escapes.
1449 switch (*LastEmitted) {
1450 default: Done = false; break;
1451 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001452 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1453 O << '$';
1454 ++LastEmitted; // Consume second '$' character.
1455 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001456 case '(': // $( -> same as GCC's { character.
1457 ++LastEmitted; // Consume '(' character.
1458 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001459 llvm_report_error("Nested variants found in inline asm string: '"
1460 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001461 }
1462 CurVariant = 0; // We're in the first variant now.
1463 break;
1464 case '|':
1465 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001466 if (CurVariant == -1)
1467 O << '|'; // this is gcc's behavior for | outside a variant
1468 else
1469 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001470 break;
1471 case ')': // $) -> same as GCC's } char.
1472 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001473 if (CurVariant == -1)
1474 O << '}'; // this is gcc's behavior for } outside a variant
1475 else
1476 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001477 break;
Chris Lattner66099132006-02-01 22:41:11 +00001478 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001479 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001480
1481 bool HasCurlyBraces = false;
1482 if (*LastEmitted == '{') { // ${variable}
1483 ++LastEmitted; // Consume '{' character.
1484 HasCurlyBraces = true;
1485 }
1486
Chris Lattner3e0cc262009-03-10 05:37:13 +00001487 // If we have ${:foo}, then this is not a real operand reference, it is a
1488 // "magic" string reference, just like in .td files. Arrange to call
1489 // PrintSpecial.
1490 if (HasCurlyBraces && *LastEmitted == ':') {
1491 ++LastEmitted;
1492 const char *StrStart = LastEmitted;
1493 const char *StrEnd = strchr(StrStart, '}');
1494 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001495 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1496 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001497 }
1498
1499 std::string Val(StrStart, StrEnd);
1500 PrintSpecial(MI, Val.c_str());
1501 LastEmitted = StrEnd+1;
1502 break;
1503 }
1504
Chris Lattner66099132006-02-01 22:41:11 +00001505 const char *IDStart = LastEmitted;
1506 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001507 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001508 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1509 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001510 llvm_report_error("Bad $ operand number in inline asm string: '"
1511 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001512 }
1513 LastEmitted = IDEnd;
1514
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001515 char Modifier[2] = { 0, 0 };
1516
Chris Lattner66099132006-02-01 22:41:11 +00001517 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001518 // If we have curly braces, check for a modifier character. This
1519 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1520 if (*LastEmitted == ':') {
1521 ++LastEmitted; // Consume ':' character.
1522 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001523 llvm_report_error("Bad ${:} expression in inline asm string: '"
1524 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001525 }
1526
1527 Modifier[0] = *LastEmitted;
1528 ++LastEmitted; // Consume modifier character.
1529 }
1530
Chris Lattner66099132006-02-01 22:41:11 +00001531 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001532 llvm_report_error("Bad ${} expression in inline asm string: '"
1533 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001534 }
1535 ++LastEmitted; // Consume '}' character.
1536 }
1537
1538 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001539 llvm_report_error("Invalid $ operand number in inline asm string: '"
1540 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001541 }
1542
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001543 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001544 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001545 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1546 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001547
1548 bool Error = false;
1549
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001550 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001551 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001552 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001553 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001554 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001555 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001556
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001557 if (OpNo >= MI->getNumOperands()) {
1558 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001559 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001560 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001561 ++OpNo; // Skip over the ID number.
1562
Chris Lattner10b318b2010-01-17 21:43:43 +00001563 if (Modifier[0] == 'l') // labels are target independent
1564 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001565 else {
1566 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001567 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001568 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1569 Modifier[0] ? Modifier : 0);
1570 } else {
1571 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1572 Modifier[0] ? Modifier : 0);
1573 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001574 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001575 }
1576 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001577 std::string msg;
1578 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001579 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001580 MI->print(Msg);
1581 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001582 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001583 }
Chris Lattner66099132006-02-01 22:41:11 +00001584 break;
1585 }
Chris Lattner66099132006-02-01 22:41:11 +00001586 }
1587 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001588 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001589}
1590
Evan Chengda47e6e2008-03-15 00:03:38 +00001591/// printImplicitDef - This method prints the specified machine instruction
1592/// that is an implicit def.
1593void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001594 if (!VerboseAsm) return;
1595 O.PadToColumn(MAI->getCommentColumn());
1596 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001597 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001598}
1599
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001600void AsmPrinter::printKill(const MachineInstr *MI) const {
1601 if (!VerboseAsm) return;
1602 O.PadToColumn(MAI->getCommentColumn());
1603 O << MAI->getCommentString() << " kill:";
1604 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1605 const MachineOperand &op = MI->getOperand(n);
1606 assert(op.isReg() && "KILL instruction must have only register operands");
1607 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1608 }
1609}
1610
Jim Laskey1ee29252007-01-26 14:34:52 +00001611/// printLabel - This method prints a local label used by debug and
1612/// exception handling tables.
1613void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001614 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001615}
1616
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001617void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001618 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001619}
1620
Chris Lattner66099132006-02-01 22:41:11 +00001621/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1622/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001623/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001624bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001625 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001626 // Target doesn't support this yet!
1627 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001628}
Chris Lattnerdd260332006-02-24 20:21:58 +00001629
1630bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1631 unsigned AsmVariant,
1632 const char *ExtraCode) {
1633 // Target doesn't support this yet!
1634 return true;
1635}
Nate Begeman37efe672006-04-22 18:53:45 +00001636
Dan Gohman29cbade2009-11-20 23:18:13 +00001637MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1638 const char *Suffix) const {
1639 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001640}
1641
1642MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001643 const BasicBlock *BB,
1644 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001645 assert(BB->hasName() &&
1646 "Address of anonymous basic block not supported yet!");
1647
Dan Gohman568a3be2009-11-05 23:14:35 +00001648 // This code must use the function name itself, and not the function number,
1649 // since it must be possible to generate the label name from within other
1650 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001651 SmallString<60> FnName;
1652 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001653
Chris Lattner2f8cc262010-01-13 07:30:49 +00001654 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001655 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001656 Mang->getNameWithPrefix(NameResult,
1657 StringRef("BA") + Twine((unsigned)FnName.size()) +
1658 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1659 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001660
Chris Lattner48130352010-01-13 06:38:18 +00001661 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001662}
1663
Chris Lattner7cb384d2009-09-12 23:02:08 +00001664MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1665 SmallString<60> Name;
1666 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1667 << getFunctionNumber() << '_' << MBBID;
1668
1669 return OutContext.GetOrCreateSymbol(Name.str());
1670}
1671
Chris Lattner6b04ede2010-01-15 23:18:17 +00001672/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1673/// value.
1674MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1675 SmallString<60> NameStr;
1676 Mang->getNameWithPrefix(NameStr, GV, false);
1677 return OutContext.GetOrCreateSymbol(NameStr.str());
1678}
1679
Chris Lattner7a2ba942010-01-16 18:37:32 +00001680/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001681/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001682/// symbol is forced to have private linkage if ForcePrivate is true.
1683MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1684 StringRef Suffix,
1685 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001686 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001687 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001688 NameStr.append(Suffix.begin(), Suffix.end());
1689 return OutContext.GetOrCreateSymbol(NameStr.str());
1690}
1691
Chris Lattner6b04ede2010-01-15 23:18:17 +00001692/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1693/// ExternalSymbol.
1694MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1695 SmallString<60> NameStr;
1696 Mang->getNameWithPrefix(NameStr, Sym);
1697 return OutContext.GetOrCreateSymbol(NameStr.str());
1698}
1699
Chris Lattner7cb384d2009-09-12 23:02:08 +00001700
Chris Lattner70a54c02009-09-13 18:25:37 +00001701/// EmitBasicBlockStart - This method prints the label for the specified
1702/// MachineBasicBlock, an alignment (if present) and a comment describing
1703/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001704void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001705 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001706 if (unsigned Align = MBB->getAlignment())
1707 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001708
Dan Gohmanb1cac332009-10-30 01:34:35 +00001709 // If the block has its address taken, emit a special label to satisfy
1710 // references to the block. This is done so that we don't need to
1711 // remember the number of this label, and so that we can make
1712 // forward references to labels without knowing what their numbers
1713 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001714 if (MBB->hasAddressTaken()) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001715 O << *GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1716 MBB->getBasicBlock());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001717 O << ':';
1718 if (VerboseAsm) {
1719 O.PadToColumn(MAI->getCommentColumn());
1720 O << MAI->getCommentString() << " Address Taken";
1721 }
1722 O << '\n';
1723 }
1724
Dan Gohmanb1cac332009-10-30 01:34:35 +00001725 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001726 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1727 if (VerboseAsm)
1728 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1729 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +00001730 O << *GetMBBSymbol(MBB->getNumber()) << ':';
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001731 if (!VerboseAsm)
1732 O << '\n';
1733 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001734
Dan Gohmanb1cac332009-10-30 01:34:35 +00001735 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001736 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001737 if (const BasicBlock *BB = MBB->getBasicBlock())
1738 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001739 O.PadToColumn(MAI->getCommentColumn());
1740 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001741 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001742 }
David Greeneb71d1b22009-08-10 16:38:07 +00001743
Chris Lattner70a54c02009-09-13 18:25:37 +00001744 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001745 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001746 }
Nate Begeman37efe672006-04-22 18:53:45 +00001747}
Nate Begeman52a51e382006-08-12 21:29:52 +00001748
Evan Chengcc415862007-11-09 01:32:10 +00001749/// printPICJumpTableSetLabel - This method prints a set label for the
1750/// specified MachineBasicBlock for a jumptable entry.
1751void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1752 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001753 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001754 return;
1755
Chris Lattner33adcfb2009-08-22 21:43:10 +00001756 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001757 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1758 << *GetMBBSymbol(MBB->getNumber())
1759 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001760 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001761}
Evan Chengd6594ae2006-09-12 21:00:35 +00001762
Evan Chengcc415862007-11-09 01:32:10 +00001763void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1764 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001765 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001766 return;
1767
Chris Lattner33adcfb2009-08-22 21:43:10 +00001768 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001769 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001770 << "_set_" << MBB->getNumber() << ','
1771 << *GetMBBSymbol(MBB->getNumber())
1772 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001773 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001774}
1775
Evan Chengd6594ae2006-09-12 21:00:35 +00001776/// printDataDirective - This method prints the asm directive for the
1777/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001778void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001779 const TargetData *TD = TM.getTargetData();
1780 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001781 case Type::FloatTyID: case Type::DoubleTyID:
1782 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1783 assert(0 && "Should have already output floating point constant.");
1784 default:
1785 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001786 case Type::IntegerTyID: {
1787 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1788 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001789 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001790 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001791 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001792 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001793 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001794 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001795 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001796 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001797 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001798 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001799 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001800 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001801 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001802 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001803 case Type::PointerTyID:
1804 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001805 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001806 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001807 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001808 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001809 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001810 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001811 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001812 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001813 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001814 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001815 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001816 }
1817}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001818
Chris Lattner53d4d782010-01-15 23:38:51 +00001819void AsmPrinter::printVisibility(const MCSymbol *Sym,
1820 unsigned Visibility) const {
1821 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001822 if (const char *Directive = MAI->getHiddenDirective())
1823 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001824 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001825 if (const char *Directive = MAI->getProtectedDirective())
1826 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001827 }
1828}
1829
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001830void AsmPrinter::printOffset(int64_t Offset) const {
1831 if (Offset > 0)
1832 O << '+' << Offset;
1833 else if (Offset < 0)
1834 O << Offset;
1835}
1836
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001837GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1838 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001839 return 0;
1840
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001841 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001842 if (GCPI != GCMetadataPrinters.end())
1843 return GCPI->second;
1844
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001845 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001846
1847 for (GCMetadataPrinterRegistry::iterator
1848 I = GCMetadataPrinterRegistry::begin(),
1849 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1850 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001851 GCMetadataPrinter *GMP = I->instantiate();
1852 GMP->S = S;
1853 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1854 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001855 }
1856
Chris Lattner103289e2009-08-23 07:19:13 +00001857 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001858 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001859}
David Greene014700c2009-07-13 20:25:48 +00001860
1861/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001862void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001863 if (!VerboseAsm)
1864 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001865
David Greene1924aab2009-11-13 21:34:57 +00001866 bool Newline = false;
1867
1868 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001869 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001870
1871 // Print source line info.
1872 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001873 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001874 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001875 // Omit the directory, because it's likely to be long and uninteresting.
1876 if (!Scope.isNull())
1877 O << Scope.getFilename();
1878 else
1879 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001880 O << ':' << DLT.getLineNumber();
1881 if (DLT.getColumnNumber() != 0)
1882 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001883 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001884 }
David Greene1924aab2009-11-13 21:34:57 +00001885
David Greeneddff9412009-11-16 15:12:23 +00001886 // Check for spills and reloads
1887 int FI;
1888
1889 const MachineFrameInfo *FrameInfo =
1890 MI.getParent()->getParent()->getFrameInfo();
1891
1892 // We assume a single instruction only has a spill or reload, not
1893 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001894 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001895 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1896 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001897 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001898 if (Newline) O << '\n';
1899 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001900 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001901 Newline = true;
1902 }
1903 }
David Greenef7ea2a52009-12-04 22:46:04 +00001904 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001905 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1906 if (Newline) O << '\n';
1907 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001908 O << MAI->getCommentString() << ' '
1909 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001910 Newline = true;
1911 }
1912 }
1913 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1914 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001915 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001916 if (Newline) O << '\n';
1917 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001918 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001919 Newline = true;
1920 }
1921 }
David Greenef7ea2a52009-12-04 22:46:04 +00001922 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001923 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1924 if (Newline) O << '\n';
1925 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001926 O << MAI->getCommentString() << ' '
1927 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001928 Newline = true;
1929 }
1930 }
1931
1932 // Check for spill-induced copies
1933 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1934 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1935 SrcSubIdx, DstSubIdx)) {
1936 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1937 if (Newline) O << '\n';
1938 O.PadToColumn(MAI->getCommentColumn());
1939 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001940 }
1941 }
David Greene014700c2009-07-13 20:25:48 +00001942}
1943
David Greenefe37ab32009-08-18 19:22:55 +00001944/// PrintChildLoopComment - Print comments about child loops within
1945/// the loop for this basic block, with nesting.
1946///
1947static void PrintChildLoopComment(formatted_raw_ostream &O,
1948 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001949 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001950 int FunctionNumber) {
1951 // Add child loop information
1952 for(MachineLoop::iterator cl = loop->begin(),
1953 clend = loop->end();
1954 cl != clend;
1955 ++cl) {
1956 MachineBasicBlock *Header = (*cl)->getHeader();
1957 assert(Header && "No header for loop");
1958
1959 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001960 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001961
Chris Lattner33adcfb2009-08-22 21:43:10 +00001962 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001963 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001964 << " Child Loop BB" << FunctionNumber << "_"
1965 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1966
Chris Lattner33adcfb2009-08-22 21:43:10 +00001967 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001968 }
1969}
1970
David Greeneb71d1b22009-08-10 16:38:07 +00001971/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00001972void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00001973 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001974 // Add loop depth information
1975 const MachineLoop *loop = LI->getLoopFor(&MBB);
1976
1977 if (loop) {
1978 // Print a newline after bb# annotation.
1979 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001980 O.PadToColumn(MAI->getCommentColumn());
1981 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001982 << '\n';
1983
Chris Lattner33adcfb2009-08-22 21:43:10 +00001984 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001985
1986 MachineBasicBlock *Header = loop->getHeader();
1987 assert(Header && "No header for loop");
1988
1989 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001990 O << MAI->getCommentString() << " Loop Header";
1991 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001992 }
1993 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001994 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001995 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1996 }
1997
1998 if (loop->empty()) {
1999 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002000 O.PadToColumn(MAI->getCommentColumn());
2001 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002002 }
2003
2004 // Add parent loop information
2005 for (const MachineLoop *CurLoop = loop->getParentLoop();
2006 CurLoop;
2007 CurLoop = CurLoop->getParentLoop()) {
2008 MachineBasicBlock *Header = CurLoop->getHeader();
2009 assert(Header && "No header for loop");
2010
2011 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002012 O.PadToColumn(MAI->getCommentColumn());
2013 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002014 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002015 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002016 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2017 }
2018 }
2019 }
2020}