blob: 415627eb9a43662c4820ffed78b8a93b3b8091ac [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"
Chris Lattner0fd90fd2010-01-22 19:52:01 +000035#include "llvm/Support/Format.h"
David Greene014700c2009-07-13 20:25:48 +000036#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000038#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000039#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000040#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000041#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000043#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000044#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000045#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000046#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000047#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000048using namespace llvm;
49
Evan Cheng42bf74b2009-03-25 01:47:28 +000050static cl::opt<cl::boolOrDefault>
51AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
52 cl::init(cl::BOU_UNSET));
53
Chris Lattner07404412010-01-22 07:06:15 +000054static bool getVerboseAsm(bool VDef) {
55 switch (AsmVerbose) {
56 default:
57 case cl::BOU_UNSET: return VDef;
58 case cl::BOU_TRUE: return true;
59 case cl::BOU_FALSE: return false;
60 }
61}
62
Devang Patel19974732007-05-03 01:11:54 +000063char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000064AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000065 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000066 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000067 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000068
69 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000070 // FIXME: Pass instprinter to streamer.
Chris Lattner16582022010-01-20 06:39:07 +000071 OutStreamer(*createAsmStreamer(OutContext, O, *T,
Chris Lattner07404412010-01-22 07:06:15 +000072 TM.getTargetData()->isLittleEndian(),
73 getVerboseAsm(VDef), 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000074
Devang Patel6b61f582010-01-16 06:09:35 +000075 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000076 DW = 0; MMI = 0;
Chris Lattner07404412010-01-22 07:06:15 +000077 VerboseAsm = getVerboseAsm(VDef);
Evan Cheng42bf74b2009-03-25 01:47:28 +000078}
Chris Lattnered138932005-12-13 06:32:10 +000079
Gordon Henriksenc317a602008-08-17 12:08:44 +000080AsmPrinter::~AsmPrinter() {
81 for (gcp_iterator I = GCMetadataPrinters.begin(),
82 E = GCMetadataPrinters.end(); I != E; ++I)
83 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000084
85 delete &OutStreamer;
86 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000087}
Chris Lattnered138932005-12-13 06:32:10 +000088
Chris Lattner38c39882009-08-03 19:12:26 +000089TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000090 return TM.getTargetLowering()->getObjFileLowering();
91}
92
Chris Lattnerdabf07c2009-08-18 06:15:16 +000093/// getCurrentSection() - Return the current section we are emitting to.
94const MCSection *AsmPrinter::getCurrentSection() const {
95 return OutStreamer.getCurrentSection();
96}
97
98
Gordon Henriksence224772008-01-07 01:30:38 +000099void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000100 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +0000101 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000102 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +0000103 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000104 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +0000105}
106
Chris Lattnera80ba712004-08-16 23:15:22 +0000107bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000108 // Initialize TargetLoweringObjectFile.
109 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
110 .Initialize(OutContext, TM);
111
Chris Lattnerc0dba722010-01-17 18:22:35 +0000112 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000113
Bob Wilson812209a2009-09-30 22:06:26 +0000114 // Allow the target to emit any magic that it wants at the start of the file.
115 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000116
Chris Lattner33adcfb2009-08-22 21:43:10 +0000117 if (MAI->hasSingleParameterDotFile()) {
Chris Lattner39248682010-01-23 05:19:23 +0000118 // Very minimal debug info. It is ignored if we emit actual
119 // debug info. If we don't, this at least helps the user find where
120 // a function came from.
Rafael Espindola952b8392008-12-03 11:01:37 +0000121 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
122 }
123
Bob Wilson812209a2009-09-30 22:06:26 +0000124 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
125 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000126 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
127 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000128 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000129
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000130 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000131 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000132 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000133 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000134 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000135
Chris Lattnerb55e0682009-09-24 05:44:53 +0000136 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
137 if (MMI)
138 MMI->AnalyzeModule(M);
139 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000140 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000141 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000142
Chris Lattnera80ba712004-08-16 23:15:22 +0000143 return false;
144}
145
Chris Lattner48d64ba2010-01-19 04:39:15 +0000146/// EmitGlobalVariable - Emit the specified global variable to the .s file.
147void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
148 if (!GV->hasInitializer()) // External globals require no code.
149 return;
150
151 // Check to see if this is a special global used by LLVM, if so, emit it.
152 if (EmitSpecialLLVMGlobal(GV))
153 return;
Chris Lattner74bfe212010-01-19 05:38:33 +0000154
155 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
156 printVisibility(GVSym, GV->getVisibility());
157
158 if (MAI->hasDotTypeDotSizeDirective()) {
159 O << "\t.type\t" << *GVSym;
160 if (MAI->getCommentString()[0] != '@')
161 O << ",@object\n";
162 else
163 O << ",%object\n";
164 }
Chris Lattner48d64ba2010-01-19 04:39:15 +0000165
Chris Lattner74bfe212010-01-19 05:38:33 +0000166 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
167
168 const TargetData *TD = TM.getTargetData();
169 unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType());
170 unsigned AlignLog = TD->getPreferredAlignmentLog(GV);
171
Chris Lattner9744d612010-01-19 05:51:42 +0000172 // Handle common and BSS local symbols (.lcomm).
173 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000174 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
175
Chris Lattner74bfe212010-01-19 05:38:33 +0000176 if (VerboseAsm) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000177 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
178 /*PrintType=*/false, GV->getParent());
179 OutStreamer.GetCommentOS() << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000180 }
Chris Lattner814819f2010-01-19 06:25:51 +0000181
182 // Handle common symbols.
Chris Lattner9744d612010-01-19 05:51:42 +0000183 if (GVKind.isCommon()) {
184 // .comm _foo, 42, 4
Chris Lattner4ed54382010-01-19 06:01:04 +0000185 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner814819f2010-01-19 06:25:51 +0000186 return;
Chris Lattner9744d612010-01-19 05:51:42 +0000187 }
Chris Lattner814819f2010-01-19 06:25:51 +0000188
189 // Handle local BSS symbols.
190 if (MAI->hasMachoZeroFillDirective()) {
191 const MCSection *TheSection =
192 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
193 // .zerofill __DATA, __bss, _foo, 400, 5
194 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
195 return;
196 }
197
198 if (const char *LComm = MAI->getLCOMMDirective()) {
199 // .lcomm _foo, 42
Chris Lattner39248682010-01-23 05:19:23 +0000200 O << LComm << *GVSym << ',' << Size << '\n';
Chris Lattner814819f2010-01-19 06:25:51 +0000201 return;
202 }
203
204 // .local _foo
Chris Lattner39248682010-01-23 05:19:23 +0000205 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Local);
Chris Lattner814819f2010-01-19 06:25:51 +0000206 // .comm _foo, 42, 4
207 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner74bfe212010-01-19 05:38:33 +0000208 return;
209 }
210
211 const MCSection *TheSection =
212 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
213
214 // Handle the zerofill directive on darwin, which is a special form of BSS
215 // emission.
216 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
217 // .globl _foo
218 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
219 // .zerofill __DATA, __common, _foo, 400, 5
220 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
221 return;
222 }
223
224 OutStreamer.SwitchSection(TheSection);
225
226 // TODO: Factor into an 'emit linkage' thing that is shared with function
227 // bodies.
228 switch (GV->getLinkage()) {
229 case GlobalValue::CommonLinkage:
230 case GlobalValue::LinkOnceAnyLinkage:
231 case GlobalValue::LinkOnceODRLinkage:
232 case GlobalValue::WeakAnyLinkage:
233 case GlobalValue::WeakODRLinkage:
234 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner4c8c6682010-01-19 06:41:24 +0000235 if (MAI->getWeakDefDirective() != 0) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000236 // .globl _foo
237 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
238 // .weak_definition _foo
Chris Lattner4c8c6682010-01-19 06:41:24 +0000239 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::WeakDefinition);
Chris Lattner74bfe212010-01-19 05:38:33 +0000240 } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
241 // .globl _foo
242 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
243 // .linkonce same_size
244 O << LinkOnce;
Chris Lattner4c8c6682010-01-19 06:41:24 +0000245 } else {
246 // .weak _foo
247 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Weak);
248 }
Chris Lattner74bfe212010-01-19 05:38:33 +0000249 break;
250 case GlobalValue::DLLExportLinkage:
251 case GlobalValue::AppendingLinkage:
252 // FIXME: appending linkage variables should go into a section of
253 // their name or something. For now, just emit them as external.
254 case GlobalValue::ExternalLinkage:
255 // If external or appending, declare as a global symbol.
256 // .globl _foo
257 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
258 break;
259 case GlobalValue::PrivateLinkage:
260 case GlobalValue::InternalLinkage:
261 break;
262 default:
263 llvm_unreachable("Unknown linkage type!");
264 }
265
266 EmitAlignment(AlignLog, GV);
Chris Lattner74bfe212010-01-19 05:38:33 +0000267 if (VerboseAsm) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000268 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
269 /*PrintType=*/false, GV->getParent());
270 OutStreamer.GetCommentOS() << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000271 }
Chris Lattner4c8c6682010-01-19 06:41:24 +0000272 OutStreamer.EmitLabel(GVSym);
Chris Lattner74bfe212010-01-19 05:38:33 +0000273
274 EmitGlobalConstant(GV->getInitializer());
275
276 if (MAI->hasDotTypeDotSizeDirective())
277 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000278
279 OutStreamer.AddBlankLine();
Chris Lattner48d64ba2010-01-19 04:39:15 +0000280}
281
282
Chris Lattnera80ba712004-08-16 23:15:22 +0000283bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000284 // Emit global variables.
285 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
286 I != E; ++I)
Chris Lattner48d64ba2010-01-19 04:39:15 +0000287 EmitGlobalVariable(I);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000288
Chris Lattner1f522fe2009-06-24 18:54:37 +0000289 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000290 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000291 DW->EndModule();
292
Chris Lattner0a7befa2009-06-24 18:52:01 +0000293 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000294 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000295 // FIXME: This is not lazy, it would be nice to only print weak references
296 // to stuff that is actually used. Note that doing so would require targets
297 // to notice uses in operands (due to constant exprs etc). This should
298 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000299
Chris Lattner0a7befa2009-06-24 18:52:01 +0000300 // Print out module-level global variables here.
301 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
302 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000303 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner39248682010-01-23 05:19:23 +0000304 OutStreamer.EmitSymbolAttribute(GetGlobalValueSymbol(I),
305 MCStreamer::WeakReference);
Chris Lattner0a7befa2009-06-24 18:52:01 +0000306 }
307
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000308 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000309 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner39248682010-01-23 05:19:23 +0000310 OutStreamer.EmitSymbolAttribute(GetGlobalValueSymbol(I),
311 MCStreamer::WeakReference);
Chris Lattner0a7befa2009-06-24 18:52:01 +0000312 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000313 }
314
Chris Lattner33adcfb2009-08-22 21:43:10 +0000315 if (MAI->getSetDirective()) {
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000316 OutStreamer.AddBlankLine();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000317 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000318 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000319 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000320
321 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000322 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000323
Chris Lattner10b318b2010-01-17 21:43:43 +0000324 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Chris Lattner39248682010-01-23 05:19:23 +0000325 OutStreamer.EmitSymbolAttribute(Name, MCStreamer::Global);
Chris Lattner10b318b2010-01-17 21:43:43 +0000326 else if (I->hasWeakLinkage())
Chris Lattner39248682010-01-23 05:19:23 +0000327 OutStreamer.EmitSymbolAttribute(Name, MCStreamer::WeakReference);
Chris Lattner10b318b2010-01-17 21:43:43 +0000328 else
Chris Lattner10595492010-01-16 01:37:14 +0000329 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000330
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000331 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000332
Chris Lattner10b318b2010-01-17 21:43:43 +0000333 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000334 }
335 }
336
Duncan Sands1465d612009-01-28 13:14:17 +0000337 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000338 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
339 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
340 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000341 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000342
Dan Gohmana779a982008-05-05 00:28:39 +0000343 // If we don't have any trampolines, then we don't require stack memory
344 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000345 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000346 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner39248682010-01-23 05:19:23 +0000347 // FIXME: This is actually a section switch on linux/x86 and systemz, use
348 // switch section.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000349 if (MAI->getNonexecutableStackDirective())
350 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000351
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000352
353 // Allow the target to emit any magic that it wants at the end of the file,
354 // after everything else has gone out.
355 EmitEndOfAsmFile(M);
356
Chris Lattnera80ba712004-08-16 23:15:22 +0000357 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000358 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000359
360 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000361 return false;
362}
363
Chris Lattner25045bd2005-11-21 07:51:36 +0000364void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000365 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000366 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000367 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000368
Chris Lattner25d812b2009-09-16 00:35:39 +0000369 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000370 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000371}
372
Evan Cheng1606e8e2009-03-13 07:51:59 +0000373namespace {
374 // SectionCPs - Keep track the alignment, constpool entries per Section.
375 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000376 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000377 unsigned Alignment;
378 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000379 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000380 };
381}
382
Chris Lattner3b4fd322005-11-21 08:25:09 +0000383/// EmitConstantPool - Print to the current output stream assembly
384/// representations of the constants in the constant pool MCP. This is
385/// used to print out constants which have been "spilled to memory" by
386/// the code generator.
387///
388void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000389 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000390 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000391
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000392 // Calculate sections for constant pool entries. We collect entries to go into
393 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000394 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000395 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000396 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000397 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000398
399 SectionKind Kind;
400 switch (CPE.getRelocationInfo()) {
401 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000402 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000403 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000404 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000405 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000406 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000407 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000408 case 4: Kind = SectionKind::getMergeableConst4(); break;
409 case 8: Kind = SectionKind::getMergeableConst8(); break;
410 case 16: Kind = SectionKind::getMergeableConst16();break;
411 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000412 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000413 }
414
Chris Lattner83d77fa2009-08-01 23:46:12 +0000415 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000416
Evan Cheng1606e8e2009-03-13 07:51:59 +0000417 // The number of sections are small, just do a linear search from the
418 // last section to the first.
419 bool Found = false;
420 unsigned SecIdx = CPSections.size();
421 while (SecIdx != 0) {
422 if (CPSections[--SecIdx].S == S) {
423 Found = true;
424 break;
425 }
426 }
427 if (!Found) {
428 SecIdx = CPSections.size();
429 CPSections.push_back(SectionCPs(S, Align));
430 }
431
432 if (Align > CPSections[SecIdx].Alignment)
433 CPSections[SecIdx].Alignment = Align;
434 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000435 }
436
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000437 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000438 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000439 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000440 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000441
Evan Cheng1606e8e2009-03-13 07:51:59 +0000442 unsigned Offset = 0;
443 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
444 unsigned CPI = CPSections[i].CPEs[j];
445 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000446
Chris Lattner3029f922006-02-09 04:46:04 +0000447 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000448 unsigned AlignMask = CPE.getAlignment() - 1;
449 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Chris Lattneraaec2052010-01-19 19:46:13 +0000450 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000451
452 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000453 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000454
Chris Lattner39248682010-01-23 05:19:23 +0000455 // Emit the label with a comment on it.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000456 if (VerboseAsm) {
Chris Lattner39248682010-01-23 05:19:23 +0000457 OutStreamer.GetCommentOS() << "constant pool ";
458 WriteTypeSymbolic(OutStreamer.GetCommentOS(), CPE.getType(),
459 MF->getFunction()->getParent());
460 OutStreamer.GetCommentOS() << '\n';
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000461 }
Chris Lattner39248682010-01-23 05:19:23 +0000462 OutStreamer.EmitLabel(GetCPISymbol(CPI));
463
Evan Cheng1606e8e2009-03-13 07:51:59 +0000464 if (CPE.isMachineConstantPoolEntry())
465 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
466 else
467 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000468 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000469 }
470}
471
Nate Begeman37efe672006-04-22 18:53:45 +0000472/// EmitJumpTableInfo - Print assembly representations of the jump tables used
473/// by the current function to the current output stream.
474///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000475void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
476 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000477 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
478 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000479
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000480 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000481
Nate Begeman2f1ae882006-07-27 01:13:04 +0000482 // Pick the directive to use to print the jump table entries, and switch to
483 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000484 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000485
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000486 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000487 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000488 if (F->isWeakForLinker() ||
489 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000490 // In PIC mode, we need to emit the jump table to the same section as the
491 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000492 // We should also do if the section name is NULL or function is declared in
493 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000494 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
495 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000496 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000497 // Otherwise, drop it in the readonly section.
498 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000499 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000500 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000501 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000502 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000503
504 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000505
Nate Begeman37efe672006-04-22 18:53:45 +0000506 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000507 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000508
509 // If this jump table was deleted, ignore it.
510 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000511
512 // For PIC codegen, if possible we want to use the SetDirective to reduce
513 // the number of relocations the assembler will generate for the jump table.
514 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000515 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000516 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000517 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000518 if (EmittedSets.insert(JTBBs[ii]))
519 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000520
Chris Lattner7c301912009-09-13 19:02:16 +0000521 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000522 // before each jump table. The first label is never referenced, but tells
523 // the assembler and linker the extents of the jump table object. The
524 // second label is actually referenced by the code.
Chris Lattner39248682010-01-23 05:19:23 +0000525 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0])
526 OutStreamer.EmitLabel(GetJTISymbol(i, true));
527
528 OutStreamer.EmitLabel(GetJTISymbol(i));
529
Nate Begeman37efe672006-04-22 18:53:45 +0000530 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000531 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000532 O << '\n';
533 }
534 }
535}
536
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000537void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
538 const MachineBasicBlock *MBB,
539 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000540 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000541
542 // Use JumpTableDirective otherwise honor the entry size from the jump table
543 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000544 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000545 bool HadJTEntryDirective = JTEntryDirective != NULL;
546 if (!HadJTEntryDirective) {
547 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000548 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000549 }
550
551 O << JTEntryDirective << ' ';
552
553 // If we have emitted set directives for the jump table entries, print
554 // them rather than the entries themselves. If we're emitting PIC, then
555 // emit the table entries as differences between two text section labels.
556 // If we're emitting non-PIC code, then emit the entries as direct
557 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000558 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000559 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000560 } else if (MAI->getSetDirective()) {
561 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000562 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000563 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000564 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000565 // If the arch uses custom Jump Table directives, don't calc relative to
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000566 // JT.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000567 if (!HadJTEntryDirective)
Chris Lattner39248682010-01-23 05:19:23 +0000568 O << '-' << *GetJTISymbol(uid);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000569 }
570}
571
572
Chris Lattnered138932005-12-13 06:32:10 +0000573/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
574/// special global used by LLVM. If so, emit it and return true, otherwise
575/// do nothing and return false.
576bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000577 if (GV->getName() == "llvm.used") {
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000578 if (MAI->hasNoDeadStrip()) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000579 EmitLLVMUsedList(GV->getInitializer());
580 return true;
581 }
582
Chris Lattner401e10c2009-07-20 06:14:25 +0000583 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000584 if (GV->getSection() == "llvm.metadata" ||
585 GV->hasAvailableExternallyLinkage())
586 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000587
588 if (!GV->hasAppendingLinkage()) return false;
589
590 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000591
Evan Cheng916d07c2007-06-04 20:39:18 +0000592 const TargetData *TD = TM.getTargetData();
593 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000594 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000595 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000596 EmitAlignment(Align, 0);
597 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000598
599 if (TM.getRelocationModel() == Reloc::Static &&
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000600 MAI->hasStaticCtorDtorReferenceInStaticMode()) {
601 StringRef Sym(".constructors_used");
602 OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
603 MCStreamer::Reference);
604 }
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000605 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000606 }
607
Chris Lattnerf231c072009-03-09 05:52:15 +0000608 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000609 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000610 EmitAlignment(Align, 0);
611 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000612
613 if (TM.getRelocationModel() == Reloc::Static &&
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000614 MAI->hasStaticCtorDtorReferenceInStaticMode()) {
615 StringRef Sym(".destructors_used");
616 OutStreamer.EmitSymbolAttribute(OutContext.GetOrCreateSymbol(Sym),
617 MCStreamer::Reference);
618 }
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000619 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000620 }
621
622 return false;
623}
624
Chris Lattner33adcfb2009-08-22 21:43:10 +0000625/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000626/// global in the specified llvm.used list for which emitUsedDirectiveFor
627/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000628void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Dan Gohmana119de82009-06-14 23:30:43 +0000629 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000630 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
631 if (InitList == 0) return;
632
633 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000634 const GlobalValue *GV =
635 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner3a9be0e2010-01-23 05:51:36 +0000636 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang))
637 OutStreamer.EmitSymbolAttribute(GetGlobalValueSymbol(GV),
638 MCStreamer::NoDeadStrip);
Chris Lattnercb05af82006-09-26 03:38:18 +0000639 }
640}
641
Chris Lattnered138932005-12-13 06:32:10 +0000642/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
643/// function pointers, ignoring the init priority.
644void AsmPrinter::EmitXXStructorList(Constant *List) {
645 // Should be an array of '{ int, void ()* }' structs. The first value is the
646 // init priority, which we ignore.
647 if (!isa<ConstantArray>(List)) return;
648 ConstantArray *InitList = cast<ConstantArray>(List);
649 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
650 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
651 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000652
653 if (CS->getOperand(1)->isNullValue())
654 return; // Found a null terminator, exit printing.
655 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000656 EmitGlobalConstant(CS->getOperand(1));
657 }
658}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000659
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000660//===--------------------------------------------------------------------===//
661// Emission and print routines
662//
663
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000664/// EmitInt8 - Emit a byte directive and value.
665///
666void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000667 OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000668}
669
670/// EmitInt16 - Emit a short directive and value.
671///
672void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000673 OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000674}
675
676/// EmitInt32 - Emit a long directive and value.
677///
678void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000679 OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000680}
681
682/// EmitInt64 - Emit a long long directive and value.
683///
684void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000685 OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000686}
687
Chris Lattner05f84532010-01-23 04:54:10 +0000688
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689/// toOctal - Convert the low order bits of X into an octal digit.
690///
691static inline char toOctal(int X) {
692 return (X&7)+'0';
693}
694
695/// printStringChar - Print a char, escaped if necessary.
696///
David Greene71847812009-07-14 20:18:05 +0000697static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000698 if (C == '"') {
699 O << "\\\"";
700 } else if (C == '\\') {
701 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000702 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000703 O << C;
704 } else {
705 switch(C) {
706 case '\b': O << "\\b"; break;
707 case '\f': O << "\\f"; break;
708 case '\n': O << "\\n"; break;
709 case '\r': O << "\\r"; break;
710 case '\t': O << "\\t"; break;
711 default:
712 O << '\\';
713 O << toOctal(C >> 6);
714 O << toOctal(C >> 3);
715 O << toOctal(C >> 0);
716 break;
717 }
718 }
719}
720
Dan Gohman189f80d2007-09-24 20:58:13 +0000721/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000722void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000723 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000724 for (unsigned i = 0, N = Name.size(); i < N; ++i)
725 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000726 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000727}
728
729
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000730//===----------------------------------------------------------------------===//
731
Chris Lattner3a420532007-05-31 18:57:45 +0000732// EmitAlignment - Emit an alignment directive to the specified power of
733// two boundary. For example, if you pass in 3 here, you will get an 8
734// byte alignment. If a global value is specified, and if that global has
735// an explicit alignment requested, it will unconditionally override the
736// alignment request. However, if ForcedAlignBits is specified, this value
737// has final say: the ultimate alignment will be the max of ForcedAlignBits
738// and the alignment computed with NumBits and the global.
739//
740// The algorithm is:
741// Align = NumBits;
742// if (GV && GV->hasalignment) Align = GV->getalignment();
743// Align = std::max(Align, ForcedAlignBits);
744//
745void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000746 unsigned ForcedAlignBits,
747 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000748 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000749 NumBits = Log2_32(GV->getAlignment());
750 NumBits = std::max(NumBits, ForcedAlignBits);
751
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000752 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000753
754 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000755 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000756 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000757
758 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000759}
David Greenea5bb59f2009-08-05 21:00:52 +0000760
Chris Lattnera80ba712004-08-16 23:15:22 +0000761// Print out the specified constant, without a storage class. Only the
762// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000763void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000764 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000765 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000766 return;
767 }
768
769 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000770 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000771 return;
772 }
773
774 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000775 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000776 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000777 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000778 return;
779 }
780
781 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000782 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000783 return;
784 }
785
786 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
787 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000788 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000789 O << '0';
790 return;
791 }
792
793 switch (CE->getOpcode()) {
794 case Instruction::ZExt:
795 case Instruction::SExt:
796 case Instruction::FPTrunc:
797 case Instruction::FPExt:
798 case Instruction::UIToFP:
799 case Instruction::SIToFP:
800 case Instruction::FPToUI:
801 case Instruction::FPToSI:
802 default:
803 llvm_unreachable("FIXME: Don't support this constant cast expr");
804 case Instruction::GetElementPtr: {
805 // generate a symbolic expression for the byte address
806 const TargetData *TD = TM.getTargetData();
807 const Constant *ptrVal = CE->getOperand(0);
808 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
809 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
810 idxVec.size());
811 if (Offset == 0)
812 return EmitConstantValueOnly(ptrVal);
813
814 // Truncate/sext the offset to the pointer size.
815 if (TD->getPointerSizeInBits() != 64) {
816 int SExtAmount = 64-TD->getPointerSizeInBits();
817 Offset = (Offset << SExtAmount) >> SExtAmount;
818 }
819
820 if (Offset)
821 O << '(';
822 EmitConstantValueOnly(ptrVal);
823 if (Offset > 0)
824 O << ") + " << Offset;
825 else
826 O << ") - " << -Offset;
827 return;
828 }
829 case Instruction::BitCast:
830 return EmitConstantValueOnly(CE->getOperand(0));
831
832 case Instruction::IntToPtr: {
833 // Handle casts to pointers by changing them into casts to the appropriate
834 // integer type. This promotes constant folding and simplifies this code.
835 const TargetData *TD = TM.getTargetData();
836 Constant *Op = CE->getOperand(0);
837 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
838 false/*ZExt*/);
839 return EmitConstantValueOnly(Op);
840 }
841
842 case Instruction::PtrToInt: {
843 // Support only foldable casts to/from pointers that can be eliminated by
844 // changing the pointer to the appropriately sized integer type.
845 Constant *Op = CE->getOperand(0);
846 const Type *Ty = CE->getType();
847 const TargetData *TD = TM.getTargetData();
848
849 // We can emit the pointer value into this slot if the slot is an
850 // integer slot greater or equal to the size of the pointer.
851 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
852 return EmitConstantValueOnly(Op);
853
854 O << "((";
855 EmitConstantValueOnly(Op);
856 APInt ptrMask =
857 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
858
859 SmallString<40> S;
860 ptrMask.toStringUnsigned(S);
861 O << ") & " << S.str() << ')';
862 return;
863 }
864
865 case Instruction::Trunc:
866 // We emit the value and depend on the assembler to truncate the generated
867 // expression properly. This is important for differences between
868 // blockaddress labels. Since the two labels are in the same function, it
869 // is reasonable to treat their delta as a 32-bit value.
870 return EmitConstantValueOnly(CE->getOperand(0));
871
872 case Instruction::Add:
873 case Instruction::Sub:
874 case Instruction::And:
875 case Instruction::Or:
876 case Instruction::Xor:
877 O << '(';
878 EmitConstantValueOnly(CE->getOperand(0));
879 O << ')';
880 switch (CE->getOpcode()) {
881 case Instruction::Add:
882 O << " + ";
883 break;
884 case Instruction::Sub:
885 O << " - ";
886 break;
887 case Instruction::And:
888 O << " & ";
889 break;
890 case Instruction::Or:
891 O << " | ";
892 break;
893 case Instruction::Xor:
894 O << " ^ ";
895 break;
896 default:
897 break;
898 }
899 O << '(';
900 EmitConstantValueOnly(CE->getOperand(1));
901 O << ')';
902 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000903 }
904}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000905
Chris Lattner91093ec2010-01-19 19:10:44 +0000906static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
907 AsmPrinter &AP) {
Chris Lattner05f84532010-01-23 04:54:10 +0000908 if (AddrSpace != 0 || !CA->isString()) {
909 // Not a string. Print the values in successive locations
Chris Lattner91093ec2010-01-19 19:10:44 +0000910 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
911 AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace);
Chris Lattner05f84532010-01-23 04:54:10 +0000912 return;
Dan Gohman00d448a2008-12-22 21:14:27 +0000913 }
Chris Lattner05f84532010-01-23 04:54:10 +0000914
915 // Otherwise, it can be emitted as .ascii.
916 SmallVector<char, 128> TmpVec;
917 TmpVec.reserve(CA->getNumOperands());
918 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
919 TmpVec.push_back(cast<ConstantInt>(CA->getOperand(i))->getZExtValue());
920
921 AP.OutStreamer.EmitBytes(StringRef(TmpVec.data(), TmpVec.size()), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000922}
923
Chris Lattner91093ec2010-01-19 19:10:44 +0000924static void EmitGlobalConstantVector(const ConstantVector *CV,
925 unsigned AddrSpace, AsmPrinter &AP) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000926 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Chris Lattner91093ec2010-01-19 19:10:44 +0000927 AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000928}
929
Chris Lattner91093ec2010-01-19 19:10:44 +0000930static void EmitGlobalConstantStruct(const ConstantStruct *CS,
931 unsigned AddrSpace, AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000932 // Print the fields in successive locations. Pad to align if needed!
Chris Lattner91093ec2010-01-19 19:10:44 +0000933 const TargetData *TD = AP.TM.getTargetData();
934 unsigned Size = TD->getTypeAllocSize(CS->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +0000935 const StructLayout *Layout = TD->getStructLayout(CS->getType());
Chris Lattner91093ec2010-01-19 19:10:44 +0000936 uint64_t SizeSoFar = 0;
937 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000938 const Constant *Field = CS->getOperand(i);
Dan Gohman00d448a2008-12-22 21:14:27 +0000939
940 // Check if padding is needed and insert one or more 0s.
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000941 uint64_t FieldSize = TD->getTypeAllocSize(Field->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +0000942 uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
943 - Layout->getElementOffset(i)) - FieldSize;
944 SizeSoFar += FieldSize + PadSize;
Dan Gohman00d448a2008-12-22 21:14:27 +0000945
946 // Now print the actual field value.
Chris Lattnerbcb83e52010-01-20 07:41:15 +0000947 AP.EmitGlobalConstant(Field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000948
949 // Insert padding - this may include padding to increase the size of the
950 // current field up to the ABI size (if the struct is not packed) as well
951 // as padding to ensure that the next field starts at the right offset.
Chris Lattner2dd245c2010-01-20 07:11:32 +0000952 AP.OutStreamer.EmitZeros(PadSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000953 }
Chris Lattner2dd245c2010-01-20 07:11:32 +0000954 assert(SizeSoFar == Layout->getSizeInBytes() &&
Dan Gohman00d448a2008-12-22 21:14:27 +0000955 "Layout of constant struct may be incorrect!");
956}
957
Chris Lattner2dd245c2010-01-20 07:11:32 +0000958static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
959 AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000960 // FP Constants are printed as integer constants to avoid losing
Chris Lattner9ceff942010-01-20 06:53:37 +0000961 // precision.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000962 if (CFP->getType()->isDoubleTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +0000963 if (AP.VerboseAsm) {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +0000964 double Val = CFP->getValueAPF().convertToDouble();
965 AP.OutStreamer.GetCommentOS() << "double " << Val << '\n';
Chris Lattner09ce6742010-01-19 22:16:33 +0000966 }
967
Chris Lattner2dd245c2010-01-20 07:11:32 +0000968 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
969 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000970 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000971 }
972
973 if (CFP->getType()->isFloatTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +0000974 if (AP.VerboseAsm) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000975 float Val = CFP->getValueAPF().convertToFloat();
976 AP.OutStreamer.GetCommentOS() << "float " << Val << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +0000977 }
Chris Lattner2dd245c2010-01-20 07:11:32 +0000978 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
979 AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000980 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000981 }
982
983 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000984 // all long double variants are printed as hex
985 // api needed to prevent premature destruction
Chris Lattner09ce6742010-01-19 22:16:33 +0000986 APInt API = CFP->getValueAPF().bitcastToAPInt();
987 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +0000988 if (AP.VerboseAsm) {
Chris Lattner72b5ebc2010-01-19 22:11:05 +0000989 // Convert to double so we can print the approximate val as a comment.
990 APFloat DoubleVal = CFP->getValueAPF();
991 bool ignored;
992 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
993 &ignored);
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000994 AP.OutStreamer.GetCommentOS() << "x86_fp80 ~= "
995 << DoubleVal.convertToDouble() << '\n';
Chris Lattner72b5ebc2010-01-19 22:11:05 +0000996 }
997
Chris Lattner2dd245c2010-01-20 07:11:32 +0000998 if (AP.TM.getTargetData()->isBigEndian()) {
999 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
1000 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001001 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001002 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1003 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001004 }
Chris Lattner9ceff942010-01-20 06:53:37 +00001005
1006 // Emit the tail padding for the long double.
Chris Lattner2dd245c2010-01-20 07:11:32 +00001007 const TargetData &TD = *AP.TM.getTargetData();
1008 AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) -
1009 TD.getTypeStoreSize(CFP->getType()), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001010 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001011 }
1012
Chris Lattner09ce6742010-01-19 22:16:33 +00001013 assert(CFP->getType()->isPPC_FP128Ty() &&
1014 "Floating point constant type not handled");
1015 // All long double variants are printed as hex api needed to prevent
1016 // premature destruction.
1017 APInt API = CFP->getValueAPF().bitcastToAPInt();
1018 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +00001019 if (AP.TM.getTargetData()->isBigEndian()) {
1020 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1021 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
Chris Lattner9ceff942010-01-20 06:53:37 +00001022 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001023 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
1024 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner09ce6742010-01-19 22:16:33 +00001025 }
Dan Gohman00d448a2008-12-22 21:14:27 +00001026}
1027
Chris Lattner2dd245c2010-01-20 07:11:32 +00001028static void EmitGlobalConstantLargeInt(const ConstantInt *CI,
1029 unsigned AddrSpace, AsmPrinter &AP) {
1030 const TargetData *TD = AP.TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001031 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001032 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001033
1034 // We don't expect assemblers to support integer data directives
1035 // for more than 64 bits, so we emit the data in at most 64-bit
1036 // quantities at a time.
1037 const uint64_t *RawData = CI->getValue().getRawData();
1038 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001039 uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i];
1040 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001041 }
1042}
1043
Chris Lattner25045bd2005-11-21 07:51:36 +00001044/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001045void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Chris Lattner043c4e52010-01-20 07:19:19 +00001046 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001047 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
Chris Lattner6449abf2010-01-19 21:51:22 +00001048 return OutStreamer.EmitZeros(Size, AddrSpace);
Chris Lattner2dd245c2010-01-20 07:11:32 +00001049 }
1050
1051 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1052 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1053 switch (Size) {
1054 case 1:
1055 case 2:
1056 case 4:
1057 case 8:
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001058 if (VerboseAsm)
1059 OutStreamer.GetCommentOS() << format("0x%llx\n", CI->getZExtValue());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001060 OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace);
1061 return;
1062 default:
1063 EmitGlobalConstantLargeInt(CI, AddrSpace, *this);
1064 return;
1065 }
1066 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001067
Chris Lattner91093ec2010-01-19 19:10:44 +00001068 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
1069 return EmitGlobalConstantArray(CVA, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001070
Chris Lattner91093ec2010-01-19 19:10:44 +00001071 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
1072 return EmitGlobalConstantStruct(CVS, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001073
Chris Lattner91093ec2010-01-19 19:10:44 +00001074 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
Chris Lattner2dd245c2010-01-20 07:11:32 +00001075 return EmitGlobalConstantFP(CFP, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001076
Chris Lattner91093ec2010-01-19 19:10:44 +00001077 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1078 return EmitGlobalConstantVector(V, AddrSpace, *this);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001079
Chris Lattnerb0bedd62010-01-20 17:57:50 +00001080 if (isa<ConstantPointerNull>(CV)) {
1081 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1082 OutStreamer.EmitIntValue(0, Size, AddrSpace);
1083 return;
1084 }
1085
Chris Lattnerea3cb402010-01-20 07:33:29 +00001086 // Otherwise, it must be a ConstantExpr. Emit the data directive, then emit
1087 // the expression value.
1088 switch (TM.getTargetData()->getTypeAllocSize(CV->getType())) {
1089 case 0: return;
1090 case 1: O << MAI->getData8bitsDirective(AddrSpace); break;
1091 case 2: O << MAI->getData16bitsDirective(AddrSpace); break;
1092 case 4: O << MAI->getData32bitsDirective(AddrSpace); break;
1093 case 8:
1094 if (const char *Dir = MAI->getData64bitsDirective(AddrSpace)) {
1095 O << Dir;
1096 break;
1097 }
1098 // FALL THROUGH.
1099 default:
1100 llvm_unreachable("Target cannot handle given data directive width!");
1101 return;
1102 }
1103
Chris Lattner25045bd2005-11-21 07:51:36 +00001104 EmitConstantValueOnly(CV);
Dan Gohmand19a53b2008-06-30 22:03:41 +00001105 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001106}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001107
Chris Lattnerfad86b02008-08-17 07:19:36 +00001108void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001109 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001111}
1112
Chris Lattner3ce9b672006-09-26 23:59:50 +00001113/// PrintSpecial - Print information related to the specified machine instr
1114/// that is independent of the operand, and may be independent of the instr
1115/// itself. This can be useful for portably encoding the comment character
1116/// or other bits of target-specific knowledge into the asmstrings. The
1117/// syntax used is ${:comment}. Targets can override this to add support
1118/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001119void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001120 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001121 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001122 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001123 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001124 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001125 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001126 // Comparing the address of MI isn't sufficient, because machineinstrs may
1127 // be allocated to the same address across functions.
1128 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1129
Owen Andersonbd58edf2009-06-24 22:28:12 +00001130 // If this is a new LastFn instruction, bump the counter.
1131 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001132 ++Counter;
1133 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001134 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001135 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001136 O << Counter;
1137 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001138 std::string msg;
1139 raw_string_ostream Msg(msg);
1140 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001141 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001142 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001143 }
1144}
1145
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001146/// processDebugLoc - Processes the debug information of each machine
1147/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001148void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1149 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001150 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1151 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001152 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001153 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001154 if (DL.isUnknown())
1155 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001156 DILocation CurDLT = MF->getDILocation(DL);
1157 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001158 return;
1159
Chris Lattner043c4e52010-01-20 07:19:19 +00001160 if (!BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001161 // After printing instruction
1162 DW->EndScope(MI);
Chris Lattner043c4e52010-01-20 07:19:19 +00001163 } else if (CurDLT.getNode() != PrevDLT) {
1164 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1165 CurDLT.getColumnNumber(),
1166 CurDLT.getScope().getNode());
1167 printLabel(L);
1168 O << '\n';
1169 DW->BeginScope(MI, L);
1170 PrevDLT = CurDLT.getNode();
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001171 }
1172}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001173
Devang Patel53bb5c92009-11-10 23:06:00 +00001174
Chris Lattner0264d1a2006-01-27 02:10:10 +00001175/// printInlineAsm - This method formats and prints the specified machine
1176/// instruction that is an inline asm.
1177void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001178 unsigned NumOperands = MI->getNumOperands();
1179
1180 // Count the number of register definitions.
1181 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001182 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001183 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001184 assert(NumDefs != NumOperands-1 && "No asm string?");
1185
Dan Gohmand735b802008-10-03 15:45:36 +00001186 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001187
1188 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001189 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001190
Dan Gohman40c57862009-11-06 00:04:54 +00001191 O << '\t';
1192
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001193 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1194 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001195 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1197 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001198 return;
1199 }
1200
Chris Lattner33adcfb2009-08-22 21:43:10 +00001201 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001202
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001203 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001204 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001205
Chris Lattner66099132006-02-01 22:41:11 +00001206 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1207 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001208
Chris Lattner66099132006-02-01 22:41:11 +00001209 while (*LastEmitted) {
1210 switch (*LastEmitted) {
1211 default: {
1212 // Not a special case, emit the string section literally.
1213 const char *LiteralEnd = LastEmitted+1;
1214 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001215 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001216 ++LiteralEnd;
1217 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1218 O.write(LastEmitted, LiteralEnd-LastEmitted);
1219 LastEmitted = LiteralEnd;
1220 break;
1221 }
Chris Lattner1c059972006-05-05 21:47:05 +00001222 case '\n':
1223 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001224 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001225 break;
Chris Lattner66099132006-02-01 22:41:11 +00001226 case '$': {
1227 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001228 bool Done = true;
1229
1230 // Handle escapes.
1231 switch (*LastEmitted) {
1232 default: Done = false; break;
1233 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001234 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1235 O << '$';
1236 ++LastEmitted; // Consume second '$' character.
1237 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001238 case '(': // $( -> same as GCC's { character.
1239 ++LastEmitted; // Consume '(' character.
1240 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001241 llvm_report_error("Nested variants found in inline asm string: '"
1242 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001243 }
1244 CurVariant = 0; // We're in the first variant now.
1245 break;
1246 case '|':
1247 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001248 if (CurVariant == -1)
1249 O << '|'; // this is gcc's behavior for | outside a variant
1250 else
1251 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001252 break;
1253 case ')': // $) -> same as GCC's } char.
1254 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001255 if (CurVariant == -1)
1256 O << '}'; // this is gcc's behavior for } outside a variant
1257 else
1258 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001259 break;
Chris Lattner66099132006-02-01 22:41:11 +00001260 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001261 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001262
1263 bool HasCurlyBraces = false;
1264 if (*LastEmitted == '{') { // ${variable}
1265 ++LastEmitted; // Consume '{' character.
1266 HasCurlyBraces = true;
1267 }
1268
Chris Lattner3e0cc262009-03-10 05:37:13 +00001269 // If we have ${:foo}, then this is not a real operand reference, it is a
1270 // "magic" string reference, just like in .td files. Arrange to call
1271 // PrintSpecial.
1272 if (HasCurlyBraces && *LastEmitted == ':') {
1273 ++LastEmitted;
1274 const char *StrStart = LastEmitted;
1275 const char *StrEnd = strchr(StrStart, '}');
1276 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001277 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1278 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001279 }
1280
1281 std::string Val(StrStart, StrEnd);
1282 PrintSpecial(MI, Val.c_str());
1283 LastEmitted = StrEnd+1;
1284 break;
1285 }
1286
Chris Lattner66099132006-02-01 22:41:11 +00001287 const char *IDStart = LastEmitted;
1288 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001289 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001290 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1291 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001292 llvm_report_error("Bad $ operand number in inline asm string: '"
1293 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001294 }
1295 LastEmitted = IDEnd;
1296
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001297 char Modifier[2] = { 0, 0 };
1298
Chris Lattner66099132006-02-01 22:41:11 +00001299 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001300 // If we have curly braces, check for a modifier character. This
1301 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1302 if (*LastEmitted == ':') {
1303 ++LastEmitted; // Consume ':' character.
1304 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001305 llvm_report_error("Bad ${:} expression in inline asm string: '"
1306 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001307 }
1308
1309 Modifier[0] = *LastEmitted;
1310 ++LastEmitted; // Consume modifier character.
1311 }
1312
Chris Lattner66099132006-02-01 22:41:11 +00001313 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001314 llvm_report_error("Bad ${} expression in inline asm string: '"
1315 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001316 }
1317 ++LastEmitted; // Consume '}' character.
1318 }
1319
1320 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001321 llvm_report_error("Invalid $ operand number in inline asm string: '"
1322 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001323 }
1324
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001325 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001326 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001327 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1328 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001329
1330 bool Error = false;
1331
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001332 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001333 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001334 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001335 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001336 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001337 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001338
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001339 if (OpNo >= MI->getNumOperands()) {
1340 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001341 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001342 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001343 ++OpNo; // Skip over the ID number.
1344
Chris Lattner10b318b2010-01-17 21:43:43 +00001345 if (Modifier[0] == 'l') // labels are target independent
1346 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001347 else {
1348 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001349 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001350 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1351 Modifier[0] ? Modifier : 0);
1352 } else {
1353 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1354 Modifier[0] ? Modifier : 0);
1355 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001356 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001357 }
1358 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001359 std::string msg;
1360 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001361 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001362 MI->print(Msg);
1363 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001364 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001365 }
Chris Lattner66099132006-02-01 22:41:11 +00001366 break;
1367 }
Chris Lattner66099132006-02-01 22:41:11 +00001368 }
1369 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001370 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001371}
1372
Evan Chengda47e6e2008-03-15 00:03:38 +00001373/// printImplicitDef - This method prints the specified machine instruction
1374/// that is an implicit def.
1375void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001376 if (!VerboseAsm) return;
1377 O.PadToColumn(MAI->getCommentColumn());
1378 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001379 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001380}
1381
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001382void AsmPrinter::printKill(const MachineInstr *MI) const {
1383 if (!VerboseAsm) return;
1384 O.PadToColumn(MAI->getCommentColumn());
1385 O << MAI->getCommentString() << " kill:";
1386 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1387 const MachineOperand &op = MI->getOperand(n);
1388 assert(op.isReg() && "KILL instruction must have only register operands");
1389 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1390 }
1391}
1392
Jim Laskey1ee29252007-01-26 14:34:52 +00001393/// printLabel - This method prints a local label used by debug and
1394/// exception handling tables.
1395void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001396 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001397}
1398
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001399void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001400 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001401}
1402
Chris Lattner66099132006-02-01 22:41:11 +00001403/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1404/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001405/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001406bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001407 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001408 // Target doesn't support this yet!
1409 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001410}
Chris Lattnerdd260332006-02-24 20:21:58 +00001411
1412bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1413 unsigned AsmVariant,
1414 const char *ExtraCode) {
1415 // Target doesn't support this yet!
1416 return true;
1417}
Nate Begeman37efe672006-04-22 18:53:45 +00001418
Dan Gohman29cbade2009-11-20 23:18:13 +00001419MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1420 const char *Suffix) const {
1421 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001422}
1423
1424MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001425 const BasicBlock *BB,
1426 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001427 assert(BB->hasName() &&
1428 "Address of anonymous basic block not supported yet!");
1429
Dan Gohman568a3be2009-11-05 23:14:35 +00001430 // This code must use the function name itself, and not the function number,
1431 // since it must be possible to generate the label name from within other
1432 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001433 SmallString<60> FnName;
1434 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001435
Chris Lattner2f8cc262010-01-13 07:30:49 +00001436 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001437 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001438 Mang->getNameWithPrefix(NameResult,
1439 StringRef("BA") + Twine((unsigned)FnName.size()) +
1440 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1441 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001442
Chris Lattner48130352010-01-13 06:38:18 +00001443 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001444}
1445
Chris Lattner7cb384d2009-09-12 23:02:08 +00001446MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1447 SmallString<60> Name;
1448 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1449 << getFunctionNumber() << '_' << MBBID;
Chris Lattner39248682010-01-23 05:19:23 +00001450 return OutContext.GetOrCreateSymbol(Name.str());
1451}
1452
1453/// GetCPISymbol - Return the symbol for the specified constant pool entry.
1454MCSymbol *AsmPrinter::GetCPISymbol(unsigned CPID) const {
1455 SmallString<60> Name;
1456 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "CPI"
1457 << getFunctionNumber() << '_' << CPID;
1458 return OutContext.GetOrCreateSymbol(Name.str());
1459}
1460
1461/// GetJTISymbol - Return the symbol for the specified jump table entry.
1462MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
1463 const char *Prefix = isLinkerPrivate ? MAI->getLinkerPrivateGlobalPrefix() :
1464 MAI->getPrivateGlobalPrefix();
1465 SmallString<60> Name;
1466 raw_svector_ostream(Name) << Prefix << "JTI" << getFunctionNumber() << '_'
1467 << JTID;
Chris Lattner7cb384d2009-09-12 23:02:08 +00001468 return OutContext.GetOrCreateSymbol(Name.str());
1469}
1470
Chris Lattner6b04ede2010-01-15 23:18:17 +00001471/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1472/// value.
1473MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1474 SmallString<60> NameStr;
1475 Mang->getNameWithPrefix(NameStr, GV, false);
1476 return OutContext.GetOrCreateSymbol(NameStr.str());
1477}
1478
Chris Lattner7a2ba942010-01-16 18:37:32 +00001479/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001480/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001481/// symbol is forced to have private linkage if ForcePrivate is true.
1482MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1483 StringRef Suffix,
1484 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001485 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001486 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001487 NameStr.append(Suffix.begin(), Suffix.end());
1488 return OutContext.GetOrCreateSymbol(NameStr.str());
1489}
1490
Chris Lattner6b04ede2010-01-15 23:18:17 +00001491/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1492/// ExternalSymbol.
1493MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1494 SmallString<60> NameStr;
1495 Mang->getNameWithPrefix(NameStr, Sym);
1496 return OutContext.GetOrCreateSymbol(NameStr.str());
1497}
1498
Chris Lattner7cb384d2009-09-12 23:02:08 +00001499
Chris Lattner523a5082010-01-22 21:50:41 +00001500
1501/// PrintParentLoopComment - Print comments about parent loops of this one.
1502static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1503 unsigned FunctionNumber) {
1504 if (Loop == 0) return;
1505 PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
1506 OS.indent(Loop->getLoopDepth()*2)
1507 << "Parent Loop BB" << FunctionNumber << "_"
1508 << Loop->getHeader()->getNumber()
1509 << " Depth=" << Loop->getLoopDepth() << '\n';
1510}
1511
1512
1513/// PrintChildLoopComment - Print comments about child loops within
1514/// the loop for this basic block, with nesting.
1515static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1516 unsigned FunctionNumber) {
1517 // Add child loop information
1518 for (MachineLoop::iterator CL = Loop->begin(), E = Loop->end();CL != E; ++CL){
1519 OS.indent((*CL)->getLoopDepth()*2)
1520 << "Child Loop BB" << FunctionNumber << "_"
1521 << (*CL)->getHeader()->getNumber() << " Depth " << (*CL)->getLoopDepth()
1522 << '\n';
1523 PrintChildLoopComment(OS, *CL, FunctionNumber);
1524 }
1525}
1526
1527/// EmitComments - Pretty-print comments for basic blocks.
1528static void PrintBasicBlockLoopComments(const MachineBasicBlock &MBB,
1529 const MachineLoopInfo *LI,
1530 const AsmPrinter &AP) {
1531 // Add loop depth information
1532 const MachineLoop *Loop = LI->getLoopFor(&MBB);
1533 if (Loop == 0) return;
1534
1535 MachineBasicBlock *Header = Loop->getHeader();
1536 assert(Header && "No header for loop");
1537
1538 // If this block is not a loop header, just print out what is the loop header
1539 // and return.
1540 if (Header != &MBB) {
1541 AP.OutStreamer.AddComment(" in Loop: Header=BB" +
1542 Twine(AP.getFunctionNumber())+"_" +
1543 Twine(Loop->getHeader()->getNumber())+
1544 " Depth="+Twine(Loop->getLoopDepth()));
1545 return;
1546 }
1547
1548 // Otherwise, it is a loop header. Print out information about child and
1549 // parent loops.
1550 raw_ostream &OS = AP.OutStreamer.GetCommentOS();
1551
1552 PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
1553
1554 OS << "=>";
1555 OS.indent(Loop->getLoopDepth()*2-2);
1556
1557 OS << "This ";
1558 if (Loop->empty())
1559 OS << "Inner ";
1560 OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
1561
1562 PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
1563}
1564
1565
Chris Lattner70a54c02009-09-13 18:25:37 +00001566/// EmitBasicBlockStart - This method prints the label for the specified
1567/// MachineBasicBlock, an alignment (if present) and a comment describing
1568/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001569void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001570 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001571 if (unsigned Align = MBB->getAlignment())
1572 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001573
Dan Gohmanb1cac332009-10-30 01:34:35 +00001574 // If the block has its address taken, emit a special label to satisfy
1575 // references to the block. This is done so that we don't need to
1576 // remember the number of this label, and so that we can make
1577 // forward references to labels without knowing what their numbers
1578 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001579 if (MBB->hasAddressTaken()) {
Chris Lattner213168b2010-01-20 07:24:05 +00001580 const BasicBlock *BB = MBB->getBasicBlock();
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001581 if (VerboseAsm)
1582 OutStreamer.AddComment("Address Taken");
Chris Lattner213168b2010-01-20 07:24:05 +00001583 OutStreamer.EmitLabel(GetBlockAddressSymbol(BB->getParent(), BB));
Dan Gohman8c2b5252009-10-30 01:27:03 +00001584 }
1585
Dan Gohmanb1cac332009-10-30 01:34:35 +00001586 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001587 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001588 if (VerboseAsm) {
Chris Lattner3a9be0e2010-01-23 05:51:36 +00001589 // NOTE: Want this comment at start of line.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001590 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001591 if (const BasicBlock *BB = MBB->getBasicBlock())
1592 if (BB->hasName())
1593 OutStreamer.AddComment("%" + BB->getName());
Chris Lattnerd8d0aee2010-01-22 21:00:45 +00001594
Chris Lattner523a5082010-01-22 21:50:41 +00001595 PrintBasicBlockLoopComments(*MBB, LI, *this);
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001596 OutStreamer.AddBlankLine();
1597 }
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001598 } else {
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001599 if (VerboseAsm) {
1600 if (const BasicBlock *BB = MBB->getBasicBlock())
1601 if (BB->hasName())
1602 OutStreamer.AddComment("%" + BB->getName());
Chris Lattner523a5082010-01-22 21:50:41 +00001603 PrintBasicBlockLoopComments(*MBB, LI, *this);
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001604 }
Chris Lattnerd8d0aee2010-01-22 21:00:45 +00001605
Chris Lattner213168b2010-01-20 07:24:05 +00001606 OutStreamer.EmitLabel(GetMBBSymbol(MBB->getNumber()));
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001607 }
Nate Begeman37efe672006-04-22 18:53:45 +00001608}
Nate Begeman52a51e382006-08-12 21:29:52 +00001609
Evan Chengcc415862007-11-09 01:32:10 +00001610/// printPICJumpTableSetLabel - This method prints a set label for the
1611/// specified MachineBasicBlock for a jumptable entry.
1612void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1613 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001614 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001615 return;
1616
Chris Lattner33adcfb2009-08-22 21:43:10 +00001617 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001618 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1619 << *GetMBBSymbol(MBB->getNumber())
Chris Lattner39248682010-01-23 05:19:23 +00001620 << '-' << *GetJTISymbol(uid) << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001621}
Evan Chengd6594ae2006-09-12 21:00:35 +00001622
Evan Chengcc415862007-11-09 01:32:10 +00001623void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1624 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001625 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001626 return;
1627
Chris Lattner33adcfb2009-08-22 21:43:10 +00001628 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001629 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001630 << "_set_" << MBB->getNumber() << ','
1631 << *GetMBBSymbol(MBB->getNumber())
1632 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001633 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001634}
1635
Chris Lattner53d4d782010-01-15 23:38:51 +00001636void AsmPrinter::printVisibility(const MCSymbol *Sym,
1637 unsigned Visibility) const {
1638 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001639 if (const char *Directive = MAI->getHiddenDirective())
1640 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001641 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001642 if (const char *Directive = MAI->getProtectedDirective())
1643 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001644 }
1645}
1646
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001647void AsmPrinter::printOffset(int64_t Offset) const {
1648 if (Offset > 0)
1649 O << '+' << Offset;
1650 else if (Offset < 0)
1651 O << Offset;
1652}
1653
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001654GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1655 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001656 return 0;
1657
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001658 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001659 if (GCPI != GCMetadataPrinters.end())
1660 return GCPI->second;
1661
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001662 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001663
1664 for (GCMetadataPrinterRegistry::iterator
1665 I = GCMetadataPrinterRegistry::begin(),
1666 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1667 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001668 GCMetadataPrinter *GMP = I->instantiate();
1669 GMP->S = S;
1670 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1671 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001672 }
1673
Chris Lattner103289e2009-08-23 07:19:13 +00001674 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001675 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001676}
David Greene014700c2009-07-13 20:25:48 +00001677
1678/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001679void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001680 if (!VerboseAsm)
1681 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001682
David Greene1924aab2009-11-13 21:34:57 +00001683 bool Newline = false;
1684
1685 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001686 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001687
1688 // Print source line info.
1689 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001690 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001691 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001692 // Omit the directory, because it's likely to be long and uninteresting.
1693 if (!Scope.isNull())
1694 O << Scope.getFilename();
1695 else
1696 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001697 O << ':' << DLT.getLineNumber();
1698 if (DLT.getColumnNumber() != 0)
1699 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001700 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001701 }
David Greene1924aab2009-11-13 21:34:57 +00001702
David Greeneddff9412009-11-16 15:12:23 +00001703 // Check for spills and reloads
1704 int FI;
1705
1706 const MachineFrameInfo *FrameInfo =
1707 MI.getParent()->getParent()->getFrameInfo();
1708
1709 // We assume a single instruction only has a spill or reload, not
1710 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001711 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001712 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1713 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001714 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001715 if (Newline) O << '\n';
1716 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001717 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001718 Newline = true;
1719 }
1720 }
David Greenef7ea2a52009-12-04 22:46:04 +00001721 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001722 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1723 if (Newline) O << '\n';
1724 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001725 O << MAI->getCommentString() << ' '
1726 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001727 Newline = true;
1728 }
1729 }
1730 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1731 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001732 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001733 if (Newline) O << '\n';
1734 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001735 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001736 Newline = true;
1737 }
1738 }
David Greenef7ea2a52009-12-04 22:46:04 +00001739 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001740 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1741 if (Newline) O << '\n';
1742 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001743 O << MAI->getCommentString() << ' '
1744 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001745 Newline = true;
1746 }
1747 }
1748
1749 // Check for spill-induced copies
1750 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1751 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1752 SrcSubIdx, DstSubIdx)) {
1753 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1754 if (Newline) O << '\n';
1755 O.PadToColumn(MAI->getCommentColumn());
1756 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001757 }
1758 }
David Greene014700c2009-07-13 20:25:48 +00001759}
1760