blob: 3f0a9def7fe3c1de0d3779aee55370c9cf08883b [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner45111d12010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000037#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000046#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000047using namespace llvm;
48
Evan Cheng42bf74b2009-03-25 01:47:28 +000049static cl::opt<cl::boolOrDefault>
50AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
51 cl::init(cl::BOU_UNSET));
52
Chris Lattner07404412010-01-22 07:06:15 +000053static bool getVerboseAsm(bool VDef) {
54 switch (AsmVerbose) {
55 default:
56 case cl::BOU_UNSET: return VDef;
57 case cl::BOU_TRUE: return true;
58 case cl::BOU_FALSE: return false;
59 }
60}
61
Devang Patel19974732007-05-03 01:11:54 +000062char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000063AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000064 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000065 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000066 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000067
68 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000069 // FIXME: Pass instprinter to streamer.
Chris Lattner16582022010-01-20 06:39:07 +000070 OutStreamer(*createAsmStreamer(OutContext, O, *T,
Chris Lattner07404412010-01-22 07:06:15 +000071 TM.getTargetData()->isLittleEndian(),
72 getVerboseAsm(VDef), 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000073
Devang Patel6b61f582010-01-16 06:09:35 +000074 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000075 DW = 0; MMI = 0;
Chris Lattner07404412010-01-22 07:06:15 +000076 VerboseAsm = getVerboseAsm(VDef);
Evan Cheng42bf74b2009-03-25 01:47:28 +000077}
Chris Lattnered138932005-12-13 06:32:10 +000078
Gordon Henriksenc317a602008-08-17 12:08:44 +000079AsmPrinter::~AsmPrinter() {
80 for (gcp_iterator I = GCMetadataPrinters.begin(),
81 E = GCMetadataPrinters.end(); I != E; ++I)
82 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000083
84 delete &OutStreamer;
85 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000086}
Chris Lattnered138932005-12-13 06:32:10 +000087
Chris Lattner38c39882009-08-03 19:12:26 +000088TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000089 return TM.getTargetLowering()->getObjFileLowering();
90}
91
Chris Lattnerdabf07c2009-08-18 06:15:16 +000092/// getCurrentSection() - Return the current section we are emitting to.
93const MCSection *AsmPrinter::getCurrentSection() const {
94 return OutStreamer.getCurrentSection();
95}
96
97
Gordon Henriksence224772008-01-07 01:30:38 +000098void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000099 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +0000100 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000101 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +0000102 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000103 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +0000104}
105
Chris Lattnera80ba712004-08-16 23:15:22 +0000106bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000107 // Initialize TargetLoweringObjectFile.
108 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
109 .Initialize(OutContext, TM);
110
Chris Lattnerc0dba722010-01-17 18:22:35 +0000111 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000112
Bob Wilson812209a2009-09-30 22:06:26 +0000113 // Allow the target to emit any magic that it wants at the start of the file.
114 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000115
Chris Lattner33adcfb2009-08-22 21:43:10 +0000116 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000117 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000118 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000119 a function came from. */
120 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
121 }
122
Bob Wilson812209a2009-09-30 22:06:26 +0000123 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
124 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000125 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
126 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000127 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000128
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000129 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000130 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000131 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000132 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000133 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000134
Chris Lattnerb55e0682009-09-24 05:44:53 +0000135 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
136 if (MMI)
137 MMI->AnalyzeModule(M);
138 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000139 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000140 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000141
Chris Lattnera80ba712004-08-16 23:15:22 +0000142 return false;
143}
144
Chris Lattner48d64ba2010-01-19 04:39:15 +0000145/// EmitGlobalVariable - Emit the specified global variable to the .s file.
146void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
147 if (!GV->hasInitializer()) // External globals require no code.
148 return;
149
150 // Check to see if this is a special global used by LLVM, if so, emit it.
151 if (EmitSpecialLLVMGlobal(GV))
152 return;
Chris Lattner74bfe212010-01-19 05:38:33 +0000153
154 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
155 printVisibility(GVSym, GV->getVisibility());
156
157 if (MAI->hasDotTypeDotSizeDirective()) {
158 O << "\t.type\t" << *GVSym;
159 if (MAI->getCommentString()[0] != '@')
160 O << ",@object\n";
161 else
162 O << ",%object\n";
163 }
Chris Lattner48d64ba2010-01-19 04:39:15 +0000164
Chris Lattner74bfe212010-01-19 05:38:33 +0000165 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
166
167 const TargetData *TD = TM.getTargetData();
168 unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType());
169 unsigned AlignLog = TD->getPreferredAlignmentLog(GV);
170
Chris Lattner9744d612010-01-19 05:51:42 +0000171 // Handle common and BSS local symbols (.lcomm).
172 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000173 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
174
Chris Lattner74bfe212010-01-19 05:38:33 +0000175 if (VerboseAsm) {
176 O.PadToColumn(MAI->getCommentColumn());
177 O << MAI->getCommentString() << ' ';
178 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattner4ed54382010-01-19 06:01:04 +0000179 O << '\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
200 O << LComm << *GVSym << ',' << Size;
201 O << '\n';
202 return;
203 }
204
205 // .local _foo
206 O << "\t.local\t" << *GVSym << '\n';
207 // .comm _foo, 42, 4
208 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner74bfe212010-01-19 05:38:33 +0000209 return;
210 }
211
212 const MCSection *TheSection =
213 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
214
215 // Handle the zerofill directive on darwin, which is a special form of BSS
216 // emission.
217 if (GVKind.isBSSExtern() && MAI->hasMachoZeroFillDirective()) {
218 // .globl _foo
219 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
220 // .zerofill __DATA, __common, _foo, 400, 5
221 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
222 return;
223 }
224
225 OutStreamer.SwitchSection(TheSection);
226
227 // TODO: Factor into an 'emit linkage' thing that is shared with function
228 // bodies.
229 switch (GV->getLinkage()) {
230 case GlobalValue::CommonLinkage:
231 case GlobalValue::LinkOnceAnyLinkage:
232 case GlobalValue::LinkOnceODRLinkage:
233 case GlobalValue::WeakAnyLinkage:
234 case GlobalValue::WeakODRLinkage:
235 case GlobalValue::LinkerPrivateLinkage:
Chris Lattner4c8c6682010-01-19 06:41:24 +0000236 if (MAI->getWeakDefDirective() != 0) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000237 // .globl _foo
238 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
239 // .weak_definition _foo
Chris Lattner4c8c6682010-01-19 06:41:24 +0000240 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::WeakDefinition);
Chris Lattner74bfe212010-01-19 05:38:33 +0000241 } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
242 // .globl _foo
243 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
244 // .linkonce same_size
245 O << LinkOnce;
Chris Lattner4c8c6682010-01-19 06:41:24 +0000246 } else {
247 // .weak _foo
248 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Weak);
249 }
Chris Lattner74bfe212010-01-19 05:38:33 +0000250 break;
251 case GlobalValue::DLLExportLinkage:
252 case GlobalValue::AppendingLinkage:
253 // FIXME: appending linkage variables should go into a section of
254 // their name or something. For now, just emit them as external.
255 case GlobalValue::ExternalLinkage:
256 // If external or appending, declare as a global symbol.
257 // .globl _foo
258 OutStreamer.EmitSymbolAttribute(GVSym, MCStreamer::Global);
259 break;
260 case GlobalValue::PrivateLinkage:
261 case GlobalValue::InternalLinkage:
262 break;
263 default:
264 llvm_unreachable("Unknown linkage type!");
265 }
266
267 EmitAlignment(AlignLog, GV);
Chris Lattner74bfe212010-01-19 05:38:33 +0000268 if (VerboseAsm) {
269 O.PadToColumn(MAI->getCommentColumn());
270 O << MAI->getCommentString() << ' ';
271 WriteAsOperand(O, GV, /*PrintType=*/false, GV->getParent());
Chris Lattner4c8c6682010-01-19 06:41:24 +0000272 O << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000273 }
Chris Lattner4c8c6682010-01-19 06:41:24 +0000274 OutStreamer.EmitLabel(GVSym);
Chris Lattner74bfe212010-01-19 05:38:33 +0000275
276 EmitGlobalConstant(GV->getInitializer());
277
278 if (MAI->hasDotTypeDotSizeDirective())
279 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
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 Lattner10b318b2010-01-17 21:43:43 +0000304 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000305 }
306
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000307 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000308 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000309 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000310 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000311 }
312
Chris Lattner33adcfb2009-08-22 21:43:10 +0000313 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000314 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000315 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000316 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000317 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000318
319 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000320 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000321
Chris Lattner10b318b2010-01-17 21:43:43 +0000322 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
323 O << "\t.globl\t" << *Name << '\n';
324 else if (I->hasWeakLinkage())
325 O << MAI->getWeakRefDirective() << *Name << '\n';
326 else
Chris Lattner10595492010-01-16 01:37:14 +0000327 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000328
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000329 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000330
Chris Lattner10b318b2010-01-17 21:43:43 +0000331 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000332 }
333 }
334
Duncan Sands1465d612009-01-28 13:14:17 +0000335 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000336 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
337 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
338 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000339 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000340
Dan Gohmana779a982008-05-05 00:28:39 +0000341 // If we don't have any trampolines, then we don't require stack memory
342 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000343 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000344 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000345 if (MAI->getNonexecutableStackDirective())
346 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000347
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000348
349 // Allow the target to emit any magic that it wants at the end of the file,
350 // after everything else has gone out.
351 EmitEndOfAsmFile(M);
352
Chris Lattnera80ba712004-08-16 23:15:22 +0000353 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000354 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000355
356 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000357 return false;
358}
359
Chris Lattner25045bd2005-11-21 07:51:36 +0000360void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000361 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000362 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000363 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000364
Chris Lattner25d812b2009-09-16 00:35:39 +0000365 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000366 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000367}
368
Evan Cheng1606e8e2009-03-13 07:51:59 +0000369namespace {
370 // SectionCPs - Keep track the alignment, constpool entries per Section.
371 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000372 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000373 unsigned Alignment;
374 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000375 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000376 };
377}
378
Chris Lattner3b4fd322005-11-21 08:25:09 +0000379/// EmitConstantPool - Print to the current output stream assembly
380/// representations of the constants in the constant pool MCP. This is
381/// used to print out constants which have been "spilled to memory" by
382/// the code generator.
383///
384void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000385 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000386 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000387
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000388 // Calculate sections for constant pool entries. We collect entries to go into
389 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000390 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000391 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000392 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000393 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000394
395 SectionKind Kind;
396 switch (CPE.getRelocationInfo()) {
397 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000398 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000399 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000400 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000401 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000402 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000403 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000404 case 4: Kind = SectionKind::getMergeableConst4(); break;
405 case 8: Kind = SectionKind::getMergeableConst8(); break;
406 case 16: Kind = SectionKind::getMergeableConst16();break;
407 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000408 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000409 }
410
Chris Lattner83d77fa2009-08-01 23:46:12 +0000411 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000412
Evan Cheng1606e8e2009-03-13 07:51:59 +0000413 // The number of sections are small, just do a linear search from the
414 // last section to the first.
415 bool Found = false;
416 unsigned SecIdx = CPSections.size();
417 while (SecIdx != 0) {
418 if (CPSections[--SecIdx].S == S) {
419 Found = true;
420 break;
421 }
422 }
423 if (!Found) {
424 SecIdx = CPSections.size();
425 CPSections.push_back(SectionCPs(S, Align));
426 }
427
428 if (Align > CPSections[SecIdx].Alignment)
429 CPSections[SecIdx].Alignment = Align;
430 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000431 }
432
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000433 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000434 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000435 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000436 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000437
Evan Cheng1606e8e2009-03-13 07:51:59 +0000438 unsigned Offset = 0;
439 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
440 unsigned CPI = CPSections[i].CPEs[j];
441 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000442
Chris Lattner3029f922006-02-09 04:46:04 +0000443 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000444 unsigned AlignMask = CPE.getAlignment() - 1;
445 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Chris Lattneraaec2052010-01-19 19:46:13 +0000446 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000447
448 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000449 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000450
Chris Lattner33adcfb2009-08-22 21:43:10 +0000451 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000452 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000453 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000454 O.PadToColumn(MAI->getCommentColumn());
455 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000456 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000457 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000458 O << '\n';
459 if (CPE.isMachineConstantPoolEntry())
460 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
461 else
462 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000463 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000464 }
465}
466
Nate Begeman37efe672006-04-22 18:53:45 +0000467/// EmitJumpTableInfo - Print assembly representations of the jump tables used
468/// by the current function to the current output stream.
469///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000470void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
471 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000472 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
473 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000474
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000475 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000476
Nate Begeman2f1ae882006-07-27 01:13:04 +0000477 // Pick the directive to use to print the jump table entries, and switch to
478 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000479 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000480
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000481 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000482 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000483 if (F->isWeakForLinker() ||
484 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000485 // In PIC mode, we need to emit the jump table to the same section as the
486 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000487 // We should also do if the section name is NULL or function is declared in
488 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000489 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
490 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000491 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000492 // Otherwise, drop it in the readonly section.
493 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000494 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000495 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000496 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000497 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000498
499 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000500
Nate Begeman37efe672006-04-22 18:53:45 +0000501 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000502 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000503
504 // If this jump table was deleted, ignore it.
505 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000506
507 // For PIC codegen, if possible we want to use the SetDirective to reduce
508 // the number of relocations the assembler will generate for the jump table.
509 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000510 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000511 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000512 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000513 if (EmittedSets.insert(JTBBs[ii]))
514 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000515
Chris Lattner7c301912009-09-13 19:02:16 +0000516 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000517 // before each jump table. The first label is never referenced, but tells
518 // the assembler and linker the extents of the jump table object. The
519 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000520 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
521 O << MAI->getLinkerPrivateGlobalPrefix()
522 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000523 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000524
Chris Lattner33adcfb2009-08-22 21:43:10 +0000525 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000526 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000527
Nate Begeman37efe672006-04-22 18:53:45 +0000528 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000529 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000530 O << '\n';
531 }
532 }
533}
534
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000535void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
536 const MachineBasicBlock *MBB,
537 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000538 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000539
540 // Use JumpTableDirective otherwise honor the entry size from the jump table
541 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000542 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000543 bool HadJTEntryDirective = JTEntryDirective != NULL;
544 if (!HadJTEntryDirective) {
545 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000546 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000547 }
548
549 O << JTEntryDirective << ' ';
550
551 // If we have emitted set directives for the jump table entries, print
552 // them rather than the entries themselves. If we're emitting PIC, then
553 // emit the table entries as differences between two text section labels.
554 // If we're emitting non-PIC code, then emit the entries as direct
555 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000556 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000557 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000558 } else if (MAI->getSetDirective()) {
559 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000560 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000561 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000562 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000563 // If the arch uses custom Jump Table directives, don't calc relative to
564 // JT
565 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000566 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000567 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000568 }
569}
570
571
Chris Lattnered138932005-12-13 06:32:10 +0000572/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
573/// special global used by LLVM. If so, emit it and return true, otherwise
574/// do nothing and return false.
575bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000576 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000577 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000578 EmitLLVMUsedList(GV->getInitializer());
579 return true;
580 }
581
Chris Lattner401e10c2009-07-20 06:14:25 +0000582 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000583 if (GV->getSection() == "llvm.metadata" ||
584 GV->hasAvailableExternallyLinkage())
585 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000586
587 if (!GV->hasAppendingLinkage()) return false;
588
589 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000590
Evan Cheng916d07c2007-06-04 20:39:18 +0000591 const TargetData *TD = TM.getTargetData();
592 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000593 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000594 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000595 EmitAlignment(Align, 0);
596 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000597
598 if (TM.getRelocationModel() == Reloc::Static &&
599 MAI->hasStaticCtorDtorReferenceInStaticMode())
600 O << ".reference .constructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000601 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000602 }
603
Chris Lattnerf231c072009-03-09 05:52:15 +0000604 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000605 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000606 EmitAlignment(Align, 0);
607 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000608
609 if (TM.getRelocationModel() == Reloc::Static &&
610 MAI->hasStaticCtorDtorReferenceInStaticMode())
611 O << ".reference .destructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000612 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000613 }
614
615 return false;
616}
617
Chris Lattner33adcfb2009-08-22 21:43:10 +0000618/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000619/// global in the specified llvm.used list for which emitUsedDirectiveFor
620/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000621void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000622 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000623
Dan Gohmana119de82009-06-14 23:30:43 +0000624 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000625 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
626 if (InitList == 0) return;
627
628 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000629 const GlobalValue *GV =
630 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000631 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000632 O << Directive;
633 EmitConstantValueOnly(InitList->getOperand(i));
634 O << '\n';
635 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000636 }
637}
638
Chris Lattnered138932005-12-13 06:32:10 +0000639/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
640/// function pointers, ignoring the init priority.
641void AsmPrinter::EmitXXStructorList(Constant *List) {
642 // Should be an array of '{ int, void ()* }' structs. The first value is the
643 // init priority, which we ignore.
644 if (!isa<ConstantArray>(List)) return;
645 ConstantArray *InitList = cast<ConstantArray>(List);
646 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
647 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
648 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000649
650 if (CS->getOperand(1)->isNullValue())
651 return; // Found a null terminator, exit printing.
652 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000653 EmitGlobalConstant(CS->getOperand(1));
654 }
655}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000656
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000657
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000658//===----------------------------------------------------------------------===//
659/// LEB 128 number encoding.
660
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000661/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000662/// representing an unsigned leb128 value.
663void AsmPrinter::PrintULEB128(unsigned Value) const {
664 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000665 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000666 Value >>= 7;
667 if (Value) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000668 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000669 if (Value) O << ", ";
670 } while (Value);
671}
672
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000673/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000674/// representing a signed leb128 value.
675void AsmPrinter::PrintSLEB128(int Value) const {
676 int Sign = Value >> (8 * sizeof(Value) - 1);
677 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000678
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000679 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000680 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000681 Value >>= 7;
682 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
683 if (IsMore) Byte |= 0x80;
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000684 PrintHex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000685 if (IsMore) O << ", ";
686 } while (IsMore);
687}
688
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689//===--------------------------------------------------------------------===//
690// Emission and print routines
691//
692
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000693/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000694///
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000695void AsmPrinter::PrintHex(uint64_t Value) const {
696 O << "0x";
697 O.write_hex(Value);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000698}
699
700/// EOL - Print a newline character to asm stream. If a comment is present
701/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000702void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000703 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000704}
Owen Anderson25995092008-07-01 21:16:27 +0000705
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000706void AsmPrinter::EOL(const Twine &Comment) const {
707 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000708 O.PadToColumn(MAI->getCommentColumn());
Chris Lattner043c4e52010-01-20 07:19:19 +0000709 O << MAI->getCommentString() << ' ' << Comment;
Owen Anderson25995092008-07-01 21:16:27 +0000710 }
711 O << '\n';
712}
713
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000714static const char *DecodeDWARFEncoding(unsigned Encoding) {
715 switch (Encoding) {
716 case dwarf::DW_EH_PE_absptr:
717 return "absptr";
718 case dwarf::DW_EH_PE_omit:
719 return "omit";
720 case dwarf::DW_EH_PE_pcrel:
721 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000722 case dwarf::DW_EH_PE_udata4:
723 return "udata4";
724 case dwarf::DW_EH_PE_udata8:
725 return "udata8";
726 case dwarf::DW_EH_PE_sdata4:
727 return "sdata4";
728 case dwarf::DW_EH_PE_sdata8:
729 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000730 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
731 return "pcrel udata4";
732 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
733 return "pcrel sdata4";
734 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
735 return "pcrel udata8";
736 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
737 return "pcrel sdata8";
738 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
739 return "indirect pcrel udata4";
740 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
741 return "indirect pcrel sdata4";
742 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
743 return "indirect pcrel udata8";
744 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
745 return "indirect pcrel sdata8";
746 }
747
748 return 0;
749}
750
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000751void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
752 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000753 O.PadToColumn(MAI->getCommentColumn());
754 O << MAI->getCommentString()
755 << ' '
756 << Comment;
757
758 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
759 O << " (" << EncStr << ')';
760 }
761 O << '\n';
762}
763
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000764/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
765/// unsigned leb128 value.
766void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000767 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000768 O << "\t.uleb128\t"
769 << Value;
770 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000771 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000772 PrintULEB128(Value);
773 }
774}
775
776/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
777/// signed leb128 value.
778void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000779 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000780 O << "\t.sleb128\t"
781 << Value;
782 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000783 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000784 PrintSLEB128(Value);
785 }
786}
787
788/// EmitInt8 - Emit a byte directive and value.
789///
790void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000791 OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000792}
793
794/// EmitInt16 - Emit a short directive and value.
795///
796void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000797 OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000798}
799
800/// EmitInt32 - Emit a long directive and value.
801///
802void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000803 OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000804}
805
806/// EmitInt64 - Emit a long long directive and value.
807///
808void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000809 OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000810}
811
812/// toOctal - Convert the low order bits of X into an octal digit.
813///
814static inline char toOctal(int X) {
815 return (X&7)+'0';
816}
817
818/// printStringChar - Print a char, escaped if necessary.
819///
David Greene71847812009-07-14 20:18:05 +0000820static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000821 if (C == '"') {
822 O << "\\\"";
823 } else if (C == '\\') {
824 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000825 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000826 O << C;
827 } else {
828 switch(C) {
829 case '\b': O << "\\b"; break;
830 case '\f': O << "\\f"; break;
831 case '\n': O << "\\n"; break;
832 case '\r': O << "\\r"; break;
833 case '\t': O << "\\t"; break;
834 default:
835 O << '\\';
836 O << toOctal(C >> 6);
837 O << toOctal(C >> 3);
838 O << toOctal(C >> 0);
839 break;
840 }
841 }
842}
843
844/// EmitString - Emit a string with quotes and a null terminator.
845/// Special characters are emitted properly.
846/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000847void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000848 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000849}
850
851void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000852 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000853 if (AscizDirective)
854 O << AscizDirective;
855 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000856 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000857 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000858 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000859 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000860 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000861 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000862 else
863 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000864}
865
866
Dan Gohman189f80d2007-09-24 20:58:13 +0000867/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000868void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000869 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000870 for (unsigned i = 0, N = Name.size(); i < N; ++i)
871 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000872 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000873}
874
875
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000876//===----------------------------------------------------------------------===//
877
Chris Lattner3a420532007-05-31 18:57:45 +0000878// EmitAlignment - Emit an alignment directive to the specified power of
879// two boundary. For example, if you pass in 3 here, you will get an 8
880// byte alignment. If a global value is specified, and if that global has
881// an explicit alignment requested, it will unconditionally override the
882// alignment request. However, if ForcedAlignBits is specified, this value
883// has final say: the ultimate alignment will be the max of ForcedAlignBits
884// and the alignment computed with NumBits and the global.
885//
886// The algorithm is:
887// Align = NumBits;
888// if (GV && GV->hasalignment) Align = GV->getalignment();
889// Align = std::max(Align, ForcedAlignBits);
890//
891void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000892 unsigned ForcedAlignBits,
893 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000894 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000895 NumBits = Log2_32(GV->getAlignment());
896 NumBits = std::max(NumBits, ForcedAlignBits);
897
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000898 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000899
900 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000901 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000902 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000903
904 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000905}
David Greenea5bb59f2009-08-05 21:00:52 +0000906
Chris Lattnera80ba712004-08-16 23:15:22 +0000907// Print out the specified constant, without a storage class. Only the
908// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000909void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000910 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000911 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000912 return;
913 }
914
915 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000916 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000917 return;
918 }
919
920 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000921 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000922 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000923 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000924 return;
925 }
926
927 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000928 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000929 return;
930 }
931
932 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
933 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000934 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000935 O << '0';
936 return;
937 }
938
939 switch (CE->getOpcode()) {
940 case Instruction::ZExt:
941 case Instruction::SExt:
942 case Instruction::FPTrunc:
943 case Instruction::FPExt:
944 case Instruction::UIToFP:
945 case Instruction::SIToFP:
946 case Instruction::FPToUI:
947 case Instruction::FPToSI:
948 default:
949 llvm_unreachable("FIXME: Don't support this constant cast expr");
950 case Instruction::GetElementPtr: {
951 // generate a symbolic expression for the byte address
952 const TargetData *TD = TM.getTargetData();
953 const Constant *ptrVal = CE->getOperand(0);
954 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
955 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
956 idxVec.size());
957 if (Offset == 0)
958 return EmitConstantValueOnly(ptrVal);
959
960 // Truncate/sext the offset to the pointer size.
961 if (TD->getPointerSizeInBits() != 64) {
962 int SExtAmount = 64-TD->getPointerSizeInBits();
963 Offset = (Offset << SExtAmount) >> SExtAmount;
964 }
965
966 if (Offset)
967 O << '(';
968 EmitConstantValueOnly(ptrVal);
969 if (Offset > 0)
970 O << ") + " << Offset;
971 else
972 O << ") - " << -Offset;
973 return;
974 }
975 case Instruction::BitCast:
976 return EmitConstantValueOnly(CE->getOperand(0));
977
978 case Instruction::IntToPtr: {
979 // Handle casts to pointers by changing them into casts to the appropriate
980 // integer type. This promotes constant folding and simplifies this code.
981 const TargetData *TD = TM.getTargetData();
982 Constant *Op = CE->getOperand(0);
983 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
984 false/*ZExt*/);
985 return EmitConstantValueOnly(Op);
986 }
987
988 case Instruction::PtrToInt: {
989 // Support only foldable casts to/from pointers that can be eliminated by
990 // changing the pointer to the appropriately sized integer type.
991 Constant *Op = CE->getOperand(0);
992 const Type *Ty = CE->getType();
993 const TargetData *TD = TM.getTargetData();
994
995 // We can emit the pointer value into this slot if the slot is an
996 // integer slot greater or equal to the size of the pointer.
997 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
998 return EmitConstantValueOnly(Op);
999
1000 O << "((";
1001 EmitConstantValueOnly(Op);
1002 APInt ptrMask =
1003 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
1004
1005 SmallString<40> S;
1006 ptrMask.toStringUnsigned(S);
1007 O << ") & " << S.str() << ')';
1008 return;
1009 }
1010
1011 case Instruction::Trunc:
1012 // We emit the value and depend on the assembler to truncate the generated
1013 // expression properly. This is important for differences between
1014 // blockaddress labels. Since the two labels are in the same function, it
1015 // is reasonable to treat their delta as a 32-bit value.
1016 return EmitConstantValueOnly(CE->getOperand(0));
1017
1018 case Instruction::Add:
1019 case Instruction::Sub:
1020 case Instruction::And:
1021 case Instruction::Or:
1022 case Instruction::Xor:
1023 O << '(';
1024 EmitConstantValueOnly(CE->getOperand(0));
1025 O << ')';
1026 switch (CE->getOpcode()) {
1027 case Instruction::Add:
1028 O << " + ";
1029 break;
1030 case Instruction::Sub:
1031 O << " - ";
1032 break;
1033 case Instruction::And:
1034 O << " & ";
1035 break;
1036 case Instruction::Or:
1037 O << " | ";
1038 break;
1039 case Instruction::Xor:
1040 O << " ^ ";
1041 break;
1042 default:
1043 break;
1044 }
1045 O << '(';
1046 EmitConstantValueOnly(CE->getOperand(1));
1047 O << ')';
1048 break;
Chris Lattnera80ba712004-08-16 23:15:22 +00001049 }
1050}
Chris Lattner1b7e2352004-08-17 06:36:49 +00001051
Chris Lattner2980cef2005-11-10 18:06:33 +00001052/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +00001053/// the predicate isString is true.
1054///
David Greene71847812009-07-14 20:18:05 +00001055static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +00001056 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +00001057 assert(CVA->isString() && "Array is not string compatible!");
1058
Dan Gohmand19a53b2008-06-30 22:03:41 +00001059 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +00001060 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001061 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +00001062 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001063 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001064 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001065 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001066}
1067
Jeff Cohenc884db42006-05-02 01:16:28 +00001068/// EmitString - Emit a zero-byte-terminated string constant.
1069///
1070void AsmPrinter::EmitString(const ConstantArray *CVA) const {
1071 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001072 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +00001073 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001074 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001075 printAsCString(O, CVA, NumElts-1);
1076 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001077 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001078 printAsCString(O, CVA, NumElts);
1079 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001080 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +00001081}
1082
Chris Lattner91093ec2010-01-19 19:10:44 +00001083static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
1084 AsmPrinter &AP) {
1085 if (AddrSpace == 0 && CA->isString()) {
1086 AP.EmitString(CA);
Dan Gohman00d448a2008-12-22 21:14:27 +00001087 } else { // Not a string. Print the values in successive locations
Chris Lattner91093ec2010-01-19 19:10:44 +00001088 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1089 AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001090 }
1091}
1092
Chris Lattner91093ec2010-01-19 19:10:44 +00001093static void EmitGlobalConstantVector(const ConstantVector *CV,
1094 unsigned AddrSpace, AsmPrinter &AP) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001095 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Chris Lattner91093ec2010-01-19 19:10:44 +00001096 AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001097}
1098
Chris Lattner91093ec2010-01-19 19:10:44 +00001099static void EmitGlobalConstantStruct(const ConstantStruct *CS,
1100 unsigned AddrSpace, AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001101 // Print the fields in successive locations. Pad to align if needed!
Chris Lattner91093ec2010-01-19 19:10:44 +00001102 const TargetData *TD = AP.TM.getTargetData();
1103 unsigned Size = TD->getTypeAllocSize(CS->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001104 const StructLayout *Layout = TD->getStructLayout(CS->getType());
Chris Lattner91093ec2010-01-19 19:10:44 +00001105 uint64_t SizeSoFar = 0;
1106 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001107 const Constant *Field = CS->getOperand(i);
Dan Gohman00d448a2008-12-22 21:14:27 +00001108
1109 // Check if padding is needed and insert one or more 0s.
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001110 uint64_t FieldSize = TD->getTypeAllocSize(Field->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001111 uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
1112 - Layout->getElementOffset(i)) - FieldSize;
1113 SizeSoFar += FieldSize + PadSize;
Dan Gohman00d448a2008-12-22 21:14:27 +00001114
1115 // Now print the actual field value.
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001116 AP.EmitGlobalConstant(Field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001117
1118 // Insert padding - this may include padding to increase the size of the
1119 // current field up to the ABI size (if the struct is not packed) as well
1120 // as padding to ensure that the next field starts at the right offset.
Chris Lattner2dd245c2010-01-20 07:11:32 +00001121 AP.OutStreamer.EmitZeros(PadSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001122 }
Chris Lattner2dd245c2010-01-20 07:11:32 +00001123 assert(SizeSoFar == Layout->getSizeInBytes() &&
Dan Gohman00d448a2008-12-22 21:14:27 +00001124 "Layout of constant struct may be incorrect!");
1125}
1126
Chris Lattner2dd245c2010-01-20 07:11:32 +00001127static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
1128 AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001129 // FP Constants are printed as integer constants to avoid losing
Chris Lattner9ceff942010-01-20 06:53:37 +00001130 // precision.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001131 if (CFP->getType()->isDoubleTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001132 if (AP.VerboseAsm) {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +00001133 double Val = CFP->getValueAPF().convertToDouble();
1134 AP.OutStreamer.GetCommentOS() << "double " << Val << '\n';
Chris Lattner09ce6742010-01-19 22:16:33 +00001135 }
1136
Chris Lattner2dd245c2010-01-20 07:11:32 +00001137 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1138 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001139 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001140 }
1141
1142 if (CFP->getType()->isFloatTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001143 if (AP.VerboseAsm) {
Chris Lattner09ce6742010-01-19 22:16:33 +00001144 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner2dd245c2010-01-20 07:11:32 +00001145 AP.O.PadToColumn(AP.MAI->getCommentColumn());
1146 AP.O << AP.MAI->getCommentString() << " float " << Val << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +00001147 }
Chris Lattner2dd245c2010-01-20 07:11:32 +00001148 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1149 AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001150 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001151 }
1152
1153 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001154 // all long double variants are printed as hex
1155 // api needed to prevent premature destruction
Chris Lattner09ce6742010-01-19 22:16:33 +00001156 APInt API = CFP->getValueAPF().bitcastToAPInt();
1157 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +00001158 if (AP.VerboseAsm) {
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001159 // Convert to double so we can print the approximate val as a comment.
1160 APFloat DoubleVal = CFP->getValueAPF();
1161 bool ignored;
1162 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1163 &ignored);
Chris Lattner2dd245c2010-01-20 07:11:32 +00001164 AP.O.PadToColumn(AP.MAI->getCommentColumn());
1165 AP.O << AP.MAI->getCommentString() << " x86_fp80 ~= "
1166 << DoubleVal.convertToDouble() << '\n';
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001167 }
1168
Chris Lattner2dd245c2010-01-20 07:11:32 +00001169 if (AP.TM.getTargetData()->isBigEndian()) {
1170 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
1171 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001172 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001173 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1174 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001175 }
Chris Lattner9ceff942010-01-20 06:53:37 +00001176
1177 // Emit the tail padding for the long double.
Chris Lattner2dd245c2010-01-20 07:11:32 +00001178 const TargetData &TD = *AP.TM.getTargetData();
1179 AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) -
1180 TD.getTypeStoreSize(CFP->getType()), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001181 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001182 }
1183
Chris Lattner09ce6742010-01-19 22:16:33 +00001184 assert(CFP->getType()->isPPC_FP128Ty() &&
1185 "Floating point constant type not handled");
1186 // All long double variants are printed as hex api needed to prevent
1187 // premature destruction.
1188 APInt API = CFP->getValueAPF().bitcastToAPInt();
1189 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +00001190 if (AP.TM.getTargetData()->isBigEndian()) {
1191 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1192 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
Chris Lattner9ceff942010-01-20 06:53:37 +00001193 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001194 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
1195 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner09ce6742010-01-19 22:16:33 +00001196 }
Dan Gohman00d448a2008-12-22 21:14:27 +00001197}
1198
Chris Lattner2dd245c2010-01-20 07:11:32 +00001199static void EmitGlobalConstantLargeInt(const ConstantInt *CI,
1200 unsigned AddrSpace, AsmPrinter &AP) {
1201 const TargetData *TD = AP.TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001202 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001203 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001204
1205 // We don't expect assemblers to support integer data directives
1206 // for more than 64 bits, so we emit the data in at most 64-bit
1207 // quantities at a time.
1208 const uint64_t *RawData = CI->getValue().getRawData();
1209 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001210 uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i];
1211 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001212 }
1213}
1214
Chris Lattner25045bd2005-11-21 07:51:36 +00001215/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001216void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Chris Lattner043c4e52010-01-20 07:19:19 +00001217 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001218 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
Chris Lattner6449abf2010-01-19 21:51:22 +00001219 return OutStreamer.EmitZeros(Size, AddrSpace);
Chris Lattner2dd245c2010-01-20 07:11:32 +00001220 }
1221
1222 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1223 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1224 switch (Size) {
1225 case 1:
1226 case 2:
1227 case 4:
1228 case 8:
1229 if (VerboseAsm) {
1230 O.PadToColumn(MAI->getCommentColumn());
1231 O << MAI->getCommentString() << " 0x";
1232 O.write_hex(CI->getZExtValue());
1233 O << '\n';
1234 }
1235 OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace);
1236 return;
1237 default:
1238 EmitGlobalConstantLargeInt(CI, AddrSpace, *this);
1239 return;
1240 }
1241 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001242
Chris Lattner91093ec2010-01-19 19:10:44 +00001243 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
1244 return EmitGlobalConstantArray(CVA, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001245
Chris Lattner91093ec2010-01-19 19:10:44 +00001246 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
1247 return EmitGlobalConstantStruct(CVS, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001248
Chris Lattner91093ec2010-01-19 19:10:44 +00001249 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
Chris Lattner2dd245c2010-01-20 07:11:32 +00001250 return EmitGlobalConstantFP(CFP, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001251
Chris Lattner91093ec2010-01-19 19:10:44 +00001252 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1253 return EmitGlobalConstantVector(V, AddrSpace, *this);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001254
Chris Lattnerb0bedd62010-01-20 17:57:50 +00001255 if (isa<ConstantPointerNull>(CV)) {
1256 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1257 OutStreamer.EmitIntValue(0, Size, AddrSpace);
1258 return;
1259 }
1260
Chris Lattnerea3cb402010-01-20 07:33:29 +00001261 // Otherwise, it must be a ConstantExpr. Emit the data directive, then emit
1262 // the expression value.
1263 switch (TM.getTargetData()->getTypeAllocSize(CV->getType())) {
1264 case 0: return;
1265 case 1: O << MAI->getData8bitsDirective(AddrSpace); break;
1266 case 2: O << MAI->getData16bitsDirective(AddrSpace); break;
1267 case 4: O << MAI->getData32bitsDirective(AddrSpace); break;
1268 case 8:
1269 if (const char *Dir = MAI->getData64bitsDirective(AddrSpace)) {
1270 O << Dir;
1271 break;
1272 }
1273 // FALL THROUGH.
1274 default:
1275 llvm_unreachable("Target cannot handle given data directive width!");
1276 return;
1277 }
1278
Chris Lattner25045bd2005-11-21 07:51:36 +00001279 EmitConstantValueOnly(CV);
Dan Gohmand19a53b2008-06-30 22:03:41 +00001280 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001281}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001282
Chris Lattnerfad86b02008-08-17 07:19:36 +00001283void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001284 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001285 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001286}
1287
Chris Lattner3ce9b672006-09-26 23:59:50 +00001288/// PrintSpecial - Print information related to the specified machine instr
1289/// that is independent of the operand, and may be independent of the instr
1290/// itself. This can be useful for portably encoding the comment character
1291/// or other bits of target-specific knowledge into the asmstrings. The
1292/// syntax used is ${:comment}. Targets can override this to add support
1293/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001294void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001295 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001296 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001297 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001298 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001299 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001300 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001301 // Comparing the address of MI isn't sufficient, because machineinstrs may
1302 // be allocated to the same address across functions.
1303 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1304
Owen Andersonbd58edf2009-06-24 22:28:12 +00001305 // If this is a new LastFn instruction, bump the counter.
1306 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001307 ++Counter;
1308 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001309 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001310 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001311 O << Counter;
1312 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001313 std::string msg;
1314 raw_string_ostream Msg(msg);
1315 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001316 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001317 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001318 }
1319}
1320
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001321/// processDebugLoc - Processes the debug information of each machine
1322/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001323void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1324 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001325 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1326 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001327 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001328 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001329 if (DL.isUnknown())
1330 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001331 DILocation CurDLT = MF->getDILocation(DL);
1332 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001333 return;
1334
Chris Lattner043c4e52010-01-20 07:19:19 +00001335 if (!BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001336 // After printing instruction
1337 DW->EndScope(MI);
Chris Lattner043c4e52010-01-20 07:19:19 +00001338 } else if (CurDLT.getNode() != PrevDLT) {
1339 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1340 CurDLT.getColumnNumber(),
1341 CurDLT.getScope().getNode());
1342 printLabel(L);
1343 O << '\n';
1344 DW->BeginScope(MI, L);
1345 PrevDLT = CurDLT.getNode();
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001346 }
1347}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001348
Devang Patel53bb5c92009-11-10 23:06:00 +00001349
Chris Lattner0264d1a2006-01-27 02:10:10 +00001350/// printInlineAsm - This method formats and prints the specified machine
1351/// instruction that is an inline asm.
1352void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001353 unsigned NumOperands = MI->getNumOperands();
1354
1355 // Count the number of register definitions.
1356 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001357 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001358 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001359 assert(NumDefs != NumOperands-1 && "No asm string?");
1360
Dan Gohmand735b802008-10-03 15:45:36 +00001361 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001362
1363 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001364 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001365
Dan Gohman40c57862009-11-06 00:04:54 +00001366 O << '\t';
1367
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001368 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1369 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001370 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001371 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1372 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001373 return;
1374 }
1375
Chris Lattner33adcfb2009-08-22 21:43:10 +00001376 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001377
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001378 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001379 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001380
Chris Lattner66099132006-02-01 22:41:11 +00001381 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1382 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001383
Chris Lattner66099132006-02-01 22:41:11 +00001384 while (*LastEmitted) {
1385 switch (*LastEmitted) {
1386 default: {
1387 // Not a special case, emit the string section literally.
1388 const char *LiteralEnd = LastEmitted+1;
1389 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001390 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001391 ++LiteralEnd;
1392 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1393 O.write(LastEmitted, LiteralEnd-LastEmitted);
1394 LastEmitted = LiteralEnd;
1395 break;
1396 }
Chris Lattner1c059972006-05-05 21:47:05 +00001397 case '\n':
1398 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001399 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001400 break;
Chris Lattner66099132006-02-01 22:41:11 +00001401 case '$': {
1402 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001403 bool Done = true;
1404
1405 // Handle escapes.
1406 switch (*LastEmitted) {
1407 default: Done = false; break;
1408 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001409 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1410 O << '$';
1411 ++LastEmitted; // Consume second '$' character.
1412 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001413 case '(': // $( -> same as GCC's { character.
1414 ++LastEmitted; // Consume '(' character.
1415 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001416 llvm_report_error("Nested variants found in inline asm string: '"
1417 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001418 }
1419 CurVariant = 0; // We're in the first variant now.
1420 break;
1421 case '|':
1422 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001423 if (CurVariant == -1)
1424 O << '|'; // this is gcc's behavior for | outside a variant
1425 else
1426 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001427 break;
1428 case ')': // $) -> same as GCC's } char.
1429 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001430 if (CurVariant == -1)
1431 O << '}'; // this is gcc's behavior for } outside a variant
1432 else
1433 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001434 break;
Chris Lattner66099132006-02-01 22:41:11 +00001435 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001436 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001437
1438 bool HasCurlyBraces = false;
1439 if (*LastEmitted == '{') { // ${variable}
1440 ++LastEmitted; // Consume '{' character.
1441 HasCurlyBraces = true;
1442 }
1443
Chris Lattner3e0cc262009-03-10 05:37:13 +00001444 // If we have ${:foo}, then this is not a real operand reference, it is a
1445 // "magic" string reference, just like in .td files. Arrange to call
1446 // PrintSpecial.
1447 if (HasCurlyBraces && *LastEmitted == ':') {
1448 ++LastEmitted;
1449 const char *StrStart = LastEmitted;
1450 const char *StrEnd = strchr(StrStart, '}');
1451 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001452 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1453 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001454 }
1455
1456 std::string Val(StrStart, StrEnd);
1457 PrintSpecial(MI, Val.c_str());
1458 LastEmitted = StrEnd+1;
1459 break;
1460 }
1461
Chris Lattner66099132006-02-01 22:41:11 +00001462 const char *IDStart = LastEmitted;
1463 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001464 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001465 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1466 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001467 llvm_report_error("Bad $ operand number in inline asm string: '"
1468 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001469 }
1470 LastEmitted = IDEnd;
1471
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001472 char Modifier[2] = { 0, 0 };
1473
Chris Lattner66099132006-02-01 22:41:11 +00001474 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001475 // If we have curly braces, check for a modifier character. This
1476 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1477 if (*LastEmitted == ':') {
1478 ++LastEmitted; // Consume ':' character.
1479 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001480 llvm_report_error("Bad ${:} expression in inline asm string: '"
1481 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001482 }
1483
1484 Modifier[0] = *LastEmitted;
1485 ++LastEmitted; // Consume modifier character.
1486 }
1487
Chris Lattner66099132006-02-01 22:41:11 +00001488 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001489 llvm_report_error("Bad ${} expression in inline asm string: '"
1490 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001491 }
1492 ++LastEmitted; // Consume '}' character.
1493 }
1494
1495 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001496 llvm_report_error("Invalid $ operand number in inline asm string: '"
1497 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001498 }
1499
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001500 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001501 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001502 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1503 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001504
1505 bool Error = false;
1506
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001507 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001508 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001509 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001510 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001511 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001512 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001513
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001514 if (OpNo >= MI->getNumOperands()) {
1515 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001516 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001517 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001518 ++OpNo; // Skip over the ID number.
1519
Chris Lattner10b318b2010-01-17 21:43:43 +00001520 if (Modifier[0] == 'l') // labels are target independent
1521 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001522 else {
1523 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001524 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001525 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1526 Modifier[0] ? Modifier : 0);
1527 } else {
1528 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1529 Modifier[0] ? Modifier : 0);
1530 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001531 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001532 }
1533 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001534 std::string msg;
1535 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001536 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001537 MI->print(Msg);
1538 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001539 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001540 }
Chris Lattner66099132006-02-01 22:41:11 +00001541 break;
1542 }
Chris Lattner66099132006-02-01 22:41:11 +00001543 }
1544 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001545 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001546}
1547
Evan Chengda47e6e2008-03-15 00:03:38 +00001548/// printImplicitDef - This method prints the specified machine instruction
1549/// that is an implicit def.
1550void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001551 if (!VerboseAsm) return;
1552 O.PadToColumn(MAI->getCommentColumn());
1553 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001554 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001555}
1556
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001557void AsmPrinter::printKill(const MachineInstr *MI) const {
1558 if (!VerboseAsm) return;
1559 O.PadToColumn(MAI->getCommentColumn());
1560 O << MAI->getCommentString() << " kill:";
1561 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1562 const MachineOperand &op = MI->getOperand(n);
1563 assert(op.isReg() && "KILL instruction must have only register operands");
1564 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1565 }
1566}
1567
Jim Laskey1ee29252007-01-26 14:34:52 +00001568/// printLabel - This method prints a local label used by debug and
1569/// exception handling tables.
1570void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001571 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001572}
1573
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001574void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001575 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001576}
1577
Chris Lattner66099132006-02-01 22:41:11 +00001578/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1579/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001580/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001581bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001582 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001583 // Target doesn't support this yet!
1584 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001585}
Chris Lattnerdd260332006-02-24 20:21:58 +00001586
1587bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1588 unsigned AsmVariant,
1589 const char *ExtraCode) {
1590 // Target doesn't support this yet!
1591 return true;
1592}
Nate Begeman37efe672006-04-22 18:53:45 +00001593
Dan Gohman29cbade2009-11-20 23:18:13 +00001594MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1595 const char *Suffix) const {
1596 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001597}
1598
1599MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001600 const BasicBlock *BB,
1601 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001602 assert(BB->hasName() &&
1603 "Address of anonymous basic block not supported yet!");
1604
Dan Gohman568a3be2009-11-05 23:14:35 +00001605 // This code must use the function name itself, and not the function number,
1606 // since it must be possible to generate the label name from within other
1607 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001608 SmallString<60> FnName;
1609 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001610
Chris Lattner2f8cc262010-01-13 07:30:49 +00001611 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001612 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001613 Mang->getNameWithPrefix(NameResult,
1614 StringRef("BA") + Twine((unsigned)FnName.size()) +
1615 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1616 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001617
Chris Lattner48130352010-01-13 06:38:18 +00001618 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001619}
1620
Chris Lattner7cb384d2009-09-12 23:02:08 +00001621MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1622 SmallString<60> Name;
1623 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1624 << getFunctionNumber() << '_' << MBBID;
1625
1626 return OutContext.GetOrCreateSymbol(Name.str());
1627}
1628
Chris Lattner6b04ede2010-01-15 23:18:17 +00001629/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1630/// value.
1631MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1632 SmallString<60> NameStr;
1633 Mang->getNameWithPrefix(NameStr, GV, false);
1634 return OutContext.GetOrCreateSymbol(NameStr.str());
1635}
1636
Chris Lattner7a2ba942010-01-16 18:37:32 +00001637/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001638/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001639/// symbol is forced to have private linkage if ForcePrivate is true.
1640MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1641 StringRef Suffix,
1642 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001643 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001644 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001645 NameStr.append(Suffix.begin(), Suffix.end());
1646 return OutContext.GetOrCreateSymbol(NameStr.str());
1647}
1648
Chris Lattner6b04ede2010-01-15 23:18:17 +00001649/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1650/// ExternalSymbol.
1651MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1652 SmallString<60> NameStr;
1653 Mang->getNameWithPrefix(NameStr, Sym);
1654 return OutContext.GetOrCreateSymbol(NameStr.str());
1655}
1656
Chris Lattner7cb384d2009-09-12 23:02:08 +00001657
Chris Lattner70a54c02009-09-13 18:25:37 +00001658/// EmitBasicBlockStart - This method prints the label for the specified
1659/// MachineBasicBlock, an alignment (if present) and a comment describing
1660/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001661void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001662 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001663 if (unsigned Align = MBB->getAlignment())
1664 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001665
Dan Gohmanb1cac332009-10-30 01:34:35 +00001666 // If the block has its address taken, emit a special label to satisfy
1667 // references to the block. This is done so that we don't need to
1668 // remember the number of this label, and so that we can make
1669 // forward references to labels without knowing what their numbers
1670 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001671 if (MBB->hasAddressTaken()) {
Chris Lattner213168b2010-01-20 07:24:05 +00001672 const BasicBlock *BB = MBB->getBasicBlock();
1673 OutStreamer.EmitLabel(GetBlockAddressSymbol(BB->getParent(), BB));
Dan Gohman8c2b5252009-10-30 01:27:03 +00001674 if (VerboseAsm) {
1675 O.PadToColumn(MAI->getCommentColumn());
Chris Lattner213168b2010-01-20 07:24:05 +00001676 O << MAI->getCommentString() << " Address Taken" << '\n';
Dan Gohman8c2b5252009-10-30 01:27:03 +00001677 }
Dan Gohman8c2b5252009-10-30 01:27:03 +00001678 }
1679
Dan Gohmanb1cac332009-10-30 01:34:35 +00001680 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001681 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1682 if (VerboseAsm)
1683 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1684 } else {
Chris Lattner213168b2010-01-20 07:24:05 +00001685 OutStreamer.EmitLabel(GetMBBSymbol(MBB->getNumber()));
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001686 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001687
Dan Gohmanb1cac332009-10-30 01:34:35 +00001688 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001689 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001690 if (const BasicBlock *BB = MBB->getBasicBlock())
1691 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001692 O.PadToColumn(MAI->getCommentColumn());
1693 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001694 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001695 }
David Greeneb71d1b22009-08-10 16:38:07 +00001696
Chris Lattner70a54c02009-09-13 18:25:37 +00001697 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001698 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001699 }
Nate Begeman37efe672006-04-22 18:53:45 +00001700}
Nate Begeman52a51e382006-08-12 21:29:52 +00001701
Evan Chengcc415862007-11-09 01:32:10 +00001702/// printPICJumpTableSetLabel - This method prints a set label for the
1703/// specified MachineBasicBlock for a jumptable entry.
1704void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1705 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001706 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001707 return;
1708
Chris Lattner33adcfb2009-08-22 21:43:10 +00001709 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001710 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1711 << *GetMBBSymbol(MBB->getNumber())
1712 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001713 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001714}
Evan Chengd6594ae2006-09-12 21:00:35 +00001715
Evan Chengcc415862007-11-09 01:32:10 +00001716void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1717 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001718 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001719 return;
1720
Chris Lattner33adcfb2009-08-22 21:43:10 +00001721 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001722 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001723 << "_set_" << MBB->getNumber() << ','
1724 << *GetMBBSymbol(MBB->getNumber())
1725 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001726 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001727}
1728
Chris Lattner53d4d782010-01-15 23:38:51 +00001729void AsmPrinter::printVisibility(const MCSymbol *Sym,
1730 unsigned Visibility) const {
1731 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001732 if (const char *Directive = MAI->getHiddenDirective())
1733 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001734 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001735 if (const char *Directive = MAI->getProtectedDirective())
1736 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001737 }
1738}
1739
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001740void AsmPrinter::printOffset(int64_t Offset) const {
1741 if (Offset > 0)
1742 O << '+' << Offset;
1743 else if (Offset < 0)
1744 O << Offset;
1745}
1746
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001747GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1748 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001749 return 0;
1750
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001751 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001752 if (GCPI != GCMetadataPrinters.end())
1753 return GCPI->second;
1754
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001755 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001756
1757 for (GCMetadataPrinterRegistry::iterator
1758 I = GCMetadataPrinterRegistry::begin(),
1759 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1760 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001761 GCMetadataPrinter *GMP = I->instantiate();
1762 GMP->S = S;
1763 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1764 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001765 }
1766
Chris Lattner103289e2009-08-23 07:19:13 +00001767 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001768 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001769}
David Greene014700c2009-07-13 20:25:48 +00001770
1771/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001772void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001773 if (!VerboseAsm)
1774 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001775
David Greene1924aab2009-11-13 21:34:57 +00001776 bool Newline = false;
1777
1778 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001779 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001780
1781 // Print source line info.
1782 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001783 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001784 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001785 // Omit the directory, because it's likely to be long and uninteresting.
1786 if (!Scope.isNull())
1787 O << Scope.getFilename();
1788 else
1789 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001790 O << ':' << DLT.getLineNumber();
1791 if (DLT.getColumnNumber() != 0)
1792 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001793 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001794 }
David Greene1924aab2009-11-13 21:34:57 +00001795
David Greeneddff9412009-11-16 15:12:23 +00001796 // Check for spills and reloads
1797 int FI;
1798
1799 const MachineFrameInfo *FrameInfo =
1800 MI.getParent()->getParent()->getFrameInfo();
1801
1802 // We assume a single instruction only has a spill or reload, not
1803 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001804 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001805 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1806 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001807 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001808 if (Newline) O << '\n';
1809 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001810 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001811 Newline = true;
1812 }
1813 }
David Greenef7ea2a52009-12-04 22:46:04 +00001814 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001815 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1816 if (Newline) O << '\n';
1817 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001818 O << MAI->getCommentString() << ' '
1819 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001820 Newline = true;
1821 }
1822 }
1823 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1824 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001825 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001826 if (Newline) O << '\n';
1827 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001828 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001829 Newline = true;
1830 }
1831 }
David Greenef7ea2a52009-12-04 22:46:04 +00001832 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001833 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1834 if (Newline) O << '\n';
1835 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001836 O << MAI->getCommentString() << ' '
1837 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001838 Newline = true;
1839 }
1840 }
1841
1842 // Check for spill-induced copies
1843 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1844 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1845 SrcSubIdx, DstSubIdx)) {
1846 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1847 if (Newline) O << '\n';
1848 O.PadToColumn(MAI->getCommentColumn());
1849 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001850 }
1851 }
David Greene014700c2009-07-13 20:25:48 +00001852}
1853
David Greenefe37ab32009-08-18 19:22:55 +00001854/// PrintChildLoopComment - Print comments about child loops within
1855/// the loop for this basic block, with nesting.
1856///
1857static void PrintChildLoopComment(formatted_raw_ostream &O,
1858 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001859 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001860 int FunctionNumber) {
1861 // Add child loop information
1862 for(MachineLoop::iterator cl = loop->begin(),
1863 clend = loop->end();
1864 cl != clend;
1865 ++cl) {
1866 MachineBasicBlock *Header = (*cl)->getHeader();
1867 assert(Header && "No header for loop");
1868
1869 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001870 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001871
Chris Lattner33adcfb2009-08-22 21:43:10 +00001872 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001873 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001874 << " Child Loop BB" << FunctionNumber << "_"
1875 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1876
Chris Lattner33adcfb2009-08-22 21:43:10 +00001877 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001878 }
1879}
1880
David Greeneb71d1b22009-08-10 16:38:07 +00001881/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00001882void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00001883 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001884 // Add loop depth information
1885 const MachineLoop *loop = LI->getLoopFor(&MBB);
1886
1887 if (loop) {
1888 // Print a newline after bb# annotation.
1889 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001890 O.PadToColumn(MAI->getCommentColumn());
1891 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001892 << '\n';
1893
Chris Lattner33adcfb2009-08-22 21:43:10 +00001894 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001895
1896 MachineBasicBlock *Header = loop->getHeader();
1897 assert(Header && "No header for loop");
1898
1899 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001900 O << MAI->getCommentString() << " Loop Header";
1901 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001902 }
1903 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001904 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001905 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1906 }
1907
1908 if (loop->empty()) {
1909 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001910 O.PadToColumn(MAI->getCommentColumn());
1911 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00001912 }
1913
1914 // Add parent loop information
1915 for (const MachineLoop *CurLoop = loop->getParentLoop();
1916 CurLoop;
1917 CurLoop = CurLoop->getParentLoop()) {
1918 MachineBasicBlock *Header = CurLoop->getHeader();
1919 assert(Header && "No header for loop");
1920
1921 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001922 O.PadToColumn(MAI->getCommentColumn());
1923 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001924 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001925 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001926 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1927 }
1928 }
1929 }
1930}