blob: b1f4423506b489a8e5d2a4f7f906883044ec0f97 [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Chris Lattner45111d12010-01-16 21:57:06 +000019#include "llvm/CodeGen/DwarfWriter.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000020#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000021#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000023#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineModuleInfo.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
Chris Lattner0fd90fd2010-01-22 19:52:01 +000035#include "llvm/Support/Format.h"
David Greene014700c2009-07-13 20:25:48 +000036#include "llvm/Support/FormattedStream.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Chris Lattner45111d12010-01-16 21:57:06 +000038#include "llvm/Target/Mangler.h"
Owen Anderson07000c62006-05-12 06:33:49 +000039#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000040#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000041#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000042#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000043#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000044#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000045#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000046#include "llvm/ADT/SmallString.h"
Chris Lattner66099132006-02-01 22:41:11 +000047#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000048using namespace llvm;
49
Evan Cheng42bf74b2009-03-25 01:47:28 +000050static cl::opt<cl::boolOrDefault>
51AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
52 cl::init(cl::BOU_UNSET));
53
Chris Lattner07404412010-01-22 07:06:15 +000054static bool getVerboseAsm(bool VDef) {
55 switch (AsmVerbose) {
56 default:
57 case cl::BOU_UNSET: return VDef;
58 case cl::BOU_TRUE: return true;
59 case cl::BOU_FALSE: return false;
60 }
61}
62
Devang Patel19974732007-05-03 01:11:54 +000063char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000064AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000065 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000066 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000067 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000068
69 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000070 // FIXME: Pass instprinter to streamer.
Chris Lattner16582022010-01-20 06:39:07 +000071 OutStreamer(*createAsmStreamer(OutContext, O, *T,
Chris Lattner07404412010-01-22 07:06:15 +000072 TM.getTargetData()->isLittleEndian(),
73 getVerboseAsm(VDef), 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000074
Devang Patel6b61f582010-01-16 06:09:35 +000075 LastMI(0), LastFn(0), Counter(~0U), PrevDLT(NULL) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000076 DW = 0; MMI = 0;
Chris Lattner07404412010-01-22 07:06:15 +000077 VerboseAsm = getVerboseAsm(VDef);
Evan Cheng42bf74b2009-03-25 01:47:28 +000078}
Chris Lattnered138932005-12-13 06:32:10 +000079
Gordon Henriksenc317a602008-08-17 12:08:44 +000080AsmPrinter::~AsmPrinter() {
81 for (gcp_iterator I = GCMetadataPrinters.begin(),
82 E = GCMetadataPrinters.end(); I != E; ++I)
83 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000084
85 delete &OutStreamer;
86 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000087}
Chris Lattnered138932005-12-13 06:32:10 +000088
Chris Lattner38c39882009-08-03 19:12:26 +000089TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000090 return TM.getTargetLowering()->getObjFileLowering();
91}
92
Chris Lattnerdabf07c2009-08-18 06:15:16 +000093/// getCurrentSection() - Return the current section we are emitting to.
94const MCSection *AsmPrinter::getCurrentSection() const {
95 return OutStreamer.getCurrentSection();
96}
97
98
Gordon Henriksence224772008-01-07 01:30:38 +000099void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +0000100 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +0000101 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000102 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +0000103 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000104 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +0000105}
106
Chris Lattnera80ba712004-08-16 23:15:22 +0000107bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000108 // Initialize TargetLoweringObjectFile.
109 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
110 .Initialize(OutContext, TM);
111
Chris Lattnerc0dba722010-01-17 18:22:35 +0000112 Mang = new Mangler(*MAI);
Chris Lattner2c1b1592006-01-23 23:47:53 +0000113
Bob Wilson812209a2009-09-30 22:06:26 +0000114 // Allow the target to emit any magic that it wants at the start of the file.
115 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000116
Chris Lattner33adcfb2009-08-22 21:43:10 +0000117 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000118 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000119 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000120 a function came from. */
121 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
122 }
123
Bob Wilson812209a2009-09-30 22:06:26 +0000124 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
125 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000126 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
127 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000128 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000129
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000130 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000131 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000132 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000133 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000134 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000135
Chris Lattnerb55e0682009-09-24 05:44:53 +0000136 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
137 if (MMI)
138 MMI->AnalyzeModule(M);
139 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000140 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000141 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000142
Chris Lattnera80ba712004-08-16 23:15:22 +0000143 return false;
144}
145
Chris Lattner48d64ba2010-01-19 04:39:15 +0000146/// EmitGlobalVariable - Emit the specified global variable to the .s file.
147void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
148 if (!GV->hasInitializer()) // External globals require no code.
149 return;
150
151 // Check to see if this is a special global used by LLVM, if so, emit it.
152 if (EmitSpecialLLVMGlobal(GV))
153 return;
Chris Lattner74bfe212010-01-19 05:38:33 +0000154
155 MCSymbol *GVSym = GetGlobalValueSymbol(GV);
156 printVisibility(GVSym, GV->getVisibility());
157
158 if (MAI->hasDotTypeDotSizeDirective()) {
159 O << "\t.type\t" << *GVSym;
160 if (MAI->getCommentString()[0] != '@')
161 O << ",@object\n";
162 else
163 O << ",%object\n";
164 }
Chris Lattner48d64ba2010-01-19 04:39:15 +0000165
Chris Lattner74bfe212010-01-19 05:38:33 +0000166 SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
167
168 const TargetData *TD = TM.getTargetData();
169 unsigned Size = TD->getTypeAllocSize(GV->getType()->getElementType());
170 unsigned AlignLog = TD->getPreferredAlignmentLog(GV);
171
Chris Lattner9744d612010-01-19 05:51:42 +0000172 // Handle common and BSS local symbols (.lcomm).
173 if (GVKind.isCommon() || GVKind.isBSSLocal()) {
Chris Lattner74bfe212010-01-19 05:38:33 +0000174 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
175
Chris Lattner74bfe212010-01-19 05:38:33 +0000176 if (VerboseAsm) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000177 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
178 /*PrintType=*/false, GV->getParent());
179 OutStreamer.GetCommentOS() << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000180 }
Chris Lattner814819f2010-01-19 06:25:51 +0000181
182 // Handle common symbols.
Chris Lattner9744d612010-01-19 05:51:42 +0000183 if (GVKind.isCommon()) {
184 // .comm _foo, 42, 4
Chris Lattner4ed54382010-01-19 06:01:04 +0000185 OutStreamer.EmitCommonSymbol(GVSym, Size, 1 << AlignLog);
Chris Lattner814819f2010-01-19 06:25:51 +0000186 return;
Chris Lattner9744d612010-01-19 05:51:42 +0000187 }
Chris Lattner814819f2010-01-19 06:25:51 +0000188
189 // Handle local BSS symbols.
190 if (MAI->hasMachoZeroFillDirective()) {
191 const MCSection *TheSection =
192 getObjFileLowering().SectionForGlobal(GV, GVKind, Mang, TM);
193 // .zerofill __DATA, __bss, _foo, 400, 5
194 OutStreamer.EmitZerofill(TheSection, GVSym, Size, 1 << AlignLog);
195 return;
196 }
197
198 if (const char *LComm = MAI->getLCOMMDirective()) {
199 // .lcomm _foo, 42
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) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000269 WriteAsOperand(OutStreamer.GetCommentOS(), GV,
270 /*PrintType=*/false, GV->getParent());
271 OutStreamer.GetCommentOS() << '\n';
Chris Lattner74bfe212010-01-19 05:38:33 +0000272 }
Chris Lattner4c8c6682010-01-19 06:41:24 +0000273 OutStreamer.EmitLabel(GVSym);
Chris Lattner74bfe212010-01-19 05:38:33 +0000274
275 EmitGlobalConstant(GV->getInitializer());
276
277 if (MAI->hasDotTypeDotSizeDirective())
278 O << "\t.size\t" << *GVSym << ", " << Size << '\n';
Chris Lattner0fd90fd2010-01-22 19:52:01 +0000279
280 OutStreamer.AddBlankLine();
Chris Lattner48d64ba2010-01-19 04:39:15 +0000281}
282
283
Chris Lattnera80ba712004-08-16 23:15:22 +0000284bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000285 // Emit global variables.
286 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
287 I != E; ++I)
Chris Lattner48d64ba2010-01-19 04:39:15 +0000288 EmitGlobalVariable(I);
Chris Lattner40bbebd2009-07-21 18:38:57 +0000289
Chris Lattner1f522fe2009-06-24 18:54:37 +0000290 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000291 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000292 DW->EndModule();
293
Chris Lattner0a7befa2009-06-24 18:52:01 +0000294 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000295 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000296 // FIXME: This is not lazy, it would be nice to only print weak references
297 // to stuff that is actually used. Note that doing so would require targets
298 // to notice uses in operands (due to constant exprs etc). This should
299 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000300
Chris Lattner0a7befa2009-06-24 18:52:01 +0000301 // Print out module-level global variables here.
302 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
303 I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000304 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000305 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000306 }
307
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000308 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner08ce3b42010-01-16 18:17:26 +0000309 if (!I->hasExternalWeakLinkage()) continue;
Chris Lattner10b318b2010-01-17 21:43:43 +0000310 O << MAI->getWeakRefDirective() << *GetGlobalValueSymbol(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000311 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000312 }
313
Chris Lattner33adcfb2009-08-22 21:43:10 +0000314 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000315 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000316 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000317 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000318 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000319
320 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000321 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000322
Chris Lattner10b318b2010-01-17 21:43:43 +0000323 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
324 O << "\t.globl\t" << *Name << '\n';
325 else if (I->hasWeakLinkage())
326 O << MAI->getWeakRefDirective() << *Name << '\n';
327 else
Chris Lattner10595492010-01-16 01:37:14 +0000328 assert(I->hasLocalLinkage() && "Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000329
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000330 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000331
Chris Lattner10b318b2010-01-17 21:43:43 +0000332 O << MAI->getSetDirective() << ' ' << *Name << ", " << *Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000333 }
334 }
335
Duncan Sands1465d612009-01-28 13:14:17 +0000336 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000337 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
338 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
339 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000340 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000341
Dan Gohmana779a982008-05-05 00:28:39 +0000342 // If we don't have any trampolines, then we don't require stack memory
343 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000344 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000345 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000346 if (MAI->getNonexecutableStackDirective())
347 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000348
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000349
350 // Allow the target to emit any magic that it wants at the end of the file,
351 // after everything else has gone out.
352 EmitEndOfAsmFile(M);
353
Chris Lattnera80ba712004-08-16 23:15:22 +0000354 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000355 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000356
357 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000358 return false;
359}
360
Chris Lattner25045bd2005-11-21 07:51:36 +0000361void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattner412c3a52010-01-16 01:24:10 +0000362 // Get the function symbol.
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000363 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000364 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000365
Chris Lattner25d812b2009-09-16 00:35:39 +0000366 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000367 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000368}
369
Evan Cheng1606e8e2009-03-13 07:51:59 +0000370namespace {
371 // SectionCPs - Keep track the alignment, constpool entries per Section.
372 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000373 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000374 unsigned Alignment;
375 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000376 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000377 };
378}
379
Chris Lattner3b4fd322005-11-21 08:25:09 +0000380/// EmitConstantPool - Print to the current output stream assembly
381/// representations of the constants in the constant pool MCP. This is
382/// used to print out constants which have been "spilled to memory" by
383/// the code generator.
384///
385void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000386 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000387 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000388
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000389 // Calculate sections for constant pool entries. We collect entries to go into
390 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000391 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000392 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000393 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000394 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000395
396 SectionKind Kind;
397 switch (CPE.getRelocationInfo()) {
398 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000399 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000400 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000401 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000402 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000403 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000404 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000405 case 4: Kind = SectionKind::getMergeableConst4(); break;
406 case 8: Kind = SectionKind::getMergeableConst8(); break;
407 case 16: Kind = SectionKind::getMergeableConst16();break;
408 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000409 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000410 }
411
Chris Lattner83d77fa2009-08-01 23:46:12 +0000412 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000413
Evan Cheng1606e8e2009-03-13 07:51:59 +0000414 // The number of sections are small, just do a linear search from the
415 // last section to the first.
416 bool Found = false;
417 unsigned SecIdx = CPSections.size();
418 while (SecIdx != 0) {
419 if (CPSections[--SecIdx].S == S) {
420 Found = true;
421 break;
422 }
423 }
424 if (!Found) {
425 SecIdx = CPSections.size();
426 CPSections.push_back(SectionCPs(S, Align));
427 }
428
429 if (Align > CPSections[SecIdx].Alignment)
430 CPSections[SecIdx].Alignment = Align;
431 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000432 }
433
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000434 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000435 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000436 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000437 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000438
Evan Cheng1606e8e2009-03-13 07:51:59 +0000439 unsigned Offset = 0;
440 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
441 unsigned CPI = CPSections[i].CPEs[j];
442 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000443
Chris Lattner3029f922006-02-09 04:46:04 +0000444 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000445 unsigned AlignMask = CPE.getAlignment() - 1;
446 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
Chris Lattneraaec2052010-01-19 19:46:13 +0000447 OutStreamer.EmitFill(NewOffset - Offset, 0/*fillval*/, 0/*addrspace*/);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000448
449 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000450 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000451
Chris Lattner33adcfb2009-08-22 21:43:10 +0000452 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000453 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000454 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000455 O.PadToColumn(MAI->getCommentColumn());
456 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000457 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000458 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000459 O << '\n';
460 if (CPE.isMachineConstantPoolEntry())
461 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
462 else
463 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000464 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000465 }
466}
467
Nate Begeman37efe672006-04-22 18:53:45 +0000468/// EmitJumpTableInfo - Print assembly representations of the jump tables used
469/// by the current function to the current output stream.
470///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000471void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
472 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000473 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
474 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000475
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000476 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000477
Nate Begeman2f1ae882006-07-27 01:13:04 +0000478 // Pick the directive to use to print the jump table entries, and switch to
479 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000480 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000481
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000482 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000483 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000484 if (F->isWeakForLinker() ||
485 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000486 // In PIC mode, we need to emit the jump table to the same section as the
487 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000488 // We should also do if the section name is NULL or function is declared in
489 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000490 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
491 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000492 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000493 // Otherwise, drop it in the readonly section.
494 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000495 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000496 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000497 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000498 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000499
500 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000501
Nate Begeman37efe672006-04-22 18:53:45 +0000502 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000503 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000504
505 // If this jump table was deleted, ignore it.
506 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000507
508 // For PIC codegen, if possible we want to use the SetDirective to reduce
509 // the number of relocations the assembler will generate for the jump table.
510 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000511 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000512 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000513 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000514 if (EmittedSets.insert(JTBBs[ii]))
515 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000516
Chris Lattner7c301912009-09-13 19:02:16 +0000517 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000518 // before each jump table. The first label is never referenced, but tells
519 // the assembler and linker the extents of the jump table object. The
520 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000521 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
522 O << MAI->getLinkerPrivateGlobalPrefix()
523 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000524 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000525
Chris Lattner33adcfb2009-08-22 21:43:10 +0000526 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000527 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000528
Nate Begeman37efe672006-04-22 18:53:45 +0000529 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000530 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000531 O << '\n';
532 }
533 }
534}
535
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000536void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
537 const MachineBasicBlock *MBB,
538 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000539 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000540
541 // Use JumpTableDirective otherwise honor the entry size from the jump table
542 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000543 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000544 bool HadJTEntryDirective = JTEntryDirective != NULL;
545 if (!HadJTEntryDirective) {
546 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000547 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000548 }
549
550 O << JTEntryDirective << ' ';
551
552 // If we have emitted set directives for the jump table entries, print
553 // them rather than the entries themselves. If we're emitting PIC, then
554 // emit the table entries as differences between two text section labels.
555 // If we're emitting non-PIC code, then emit the entries as direct
556 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000557 if (!isPIC) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000558 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattner33adcfb2009-08-22 21:43:10 +0000559 } else if (MAI->getSetDirective()) {
560 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000561 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000562 } else {
Chris Lattner10b318b2010-01-17 21:43:43 +0000563 O << *GetMBBSymbol(MBB->getNumber());
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000564 // If the arch uses custom Jump Table directives, don't calc relative to
565 // JT
566 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000567 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000568 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000569 }
570}
571
572
Chris Lattnered138932005-12-13 06:32:10 +0000573/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
574/// special global used by LLVM. If so, emit it and return true, otherwise
575/// do nothing and return false.
576bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000577 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000578 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000579 EmitLLVMUsedList(GV->getInitializer());
580 return true;
581 }
582
Chris Lattner401e10c2009-07-20 06:14:25 +0000583 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000584 if (GV->getSection() == "llvm.metadata" ||
585 GV->hasAvailableExternallyLinkage())
586 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000587
588 if (!GV->hasAppendingLinkage()) return false;
589
590 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000591
Evan Cheng916d07c2007-06-04 20:39:18 +0000592 const TargetData *TD = TM.getTargetData();
593 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000594 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000595 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000596 EmitAlignment(Align, 0);
597 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000598
599 if (TM.getRelocationModel() == Reloc::Static &&
600 MAI->hasStaticCtorDtorReferenceInStaticMode())
601 O << ".reference .constructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000602 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000603 }
604
Chris Lattnerf231c072009-03-09 05:52:15 +0000605 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000606 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000607 EmitAlignment(Align, 0);
608 EmitXXStructorList(GV->getInitializer());
Chris Lattner71eae712010-01-19 04:34:02 +0000609
610 if (TM.getRelocationModel() == Reloc::Static &&
611 MAI->hasStaticCtorDtorReferenceInStaticMode())
612 O << ".reference .destructors_used\n";
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000613 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000614 }
615
616 return false;
617}
618
Chris Lattner33adcfb2009-08-22 21:43:10 +0000619/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000620/// global in the specified llvm.used list for which emitUsedDirectiveFor
621/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000622void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000623 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000624
Dan Gohmana119de82009-06-14 23:30:43 +0000625 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000626 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
627 if (InitList == 0) return;
628
629 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000630 const GlobalValue *GV =
631 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000632 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000633 O << Directive;
634 EmitConstantValueOnly(InitList->getOperand(i));
635 O << '\n';
636 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000637 }
638}
639
Chris Lattnered138932005-12-13 06:32:10 +0000640/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
641/// function pointers, ignoring the init priority.
642void AsmPrinter::EmitXXStructorList(Constant *List) {
643 // Should be an array of '{ int, void ()* }' structs. The first value is the
644 // init priority, which we ignore.
645 if (!isa<ConstantArray>(List)) return;
646 ConstantArray *InitList = cast<ConstantArray>(List);
647 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
648 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
649 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000650
651 if (CS->getOperand(1)->isNullValue())
652 return; // Found a null terminator, exit printing.
653 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000654 EmitGlobalConstant(CS->getOperand(1));
655 }
656}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000657
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000658
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000659//===----------------------------------------------------------------------===//
660/// LEB 128 number encoding.
661
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000662/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000663/// representing an unsigned leb128 value.
664void AsmPrinter::PrintULEB128(unsigned Value) const {
665 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000666 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000667 Value >>= 7;
668 if (Value) Byte |= 0x80;
Chris Lattner05953242010-01-22 21:57:56 +0000669 O << "0x";
670 O.write_hex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000671 if (Value) O << ", ";
672 } while (Value);
673}
674
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000675/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000676/// representing a signed leb128 value.
677void AsmPrinter::PrintSLEB128(int Value) const {
678 int Sign = Value >> (8 * sizeof(Value) - 1);
679 bool IsMore;
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000680
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000681 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000682 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000683 Value >>= 7;
684 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
685 if (IsMore) Byte |= 0x80;
Chris Lattner05953242010-01-22 21:57:56 +0000686 O << "0x";
687 O.write_hex(Byte);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000688 if (IsMore) O << ", ";
689 } while (IsMore);
690}
691
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000692//===--------------------------------------------------------------------===//
693// Emission and print routines
694//
695
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000696/// EOL - Print a newline character to asm stream. If a comment is present
697/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000698void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000699 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000700}
Owen Anderson25995092008-07-01 21:16:27 +0000701
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000702void AsmPrinter::EOL(const Twine &Comment) const {
703 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000704 O.PadToColumn(MAI->getCommentColumn());
Chris Lattner043c4e52010-01-20 07:19:19 +0000705 O << MAI->getCommentString() << ' ' << Comment;
Owen Anderson25995092008-07-01 21:16:27 +0000706 }
707 O << '\n';
708}
709
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000710static const char *DecodeDWARFEncoding(unsigned Encoding) {
711 switch (Encoding) {
712 case dwarf::DW_EH_PE_absptr:
713 return "absptr";
714 case dwarf::DW_EH_PE_omit:
715 return "omit";
716 case dwarf::DW_EH_PE_pcrel:
717 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000718 case dwarf::DW_EH_PE_udata4:
719 return "udata4";
720 case dwarf::DW_EH_PE_udata8:
721 return "udata8";
722 case dwarf::DW_EH_PE_sdata4:
723 return "sdata4";
724 case dwarf::DW_EH_PE_sdata8:
725 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000726 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
727 return "pcrel udata4";
728 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
729 return "pcrel sdata4";
730 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
731 return "pcrel udata8";
732 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
733 return "pcrel sdata8";
734 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
735 return "indirect pcrel udata4";
736 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
737 return "indirect pcrel sdata4";
738 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
739 return "indirect pcrel udata8";
740 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
741 return "indirect pcrel sdata8";
742 }
743
744 return 0;
745}
746
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000747void AsmPrinter::EOL(const Twine &Comment, unsigned Encoding) const {
748 if (VerboseAsm && !Comment.isTriviallyEmpty()) {
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000749 O.PadToColumn(MAI->getCommentColumn());
750 O << MAI->getCommentString()
751 << ' '
752 << Comment;
753
754 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
755 O << " (" << EncStr << ')';
756 }
757 O << '\n';
758}
759
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000760/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
761/// unsigned leb128 value.
762void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000763 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000764 O << "\t.uleb128\t"
765 << Value;
766 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000767 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000768 PrintULEB128(Value);
769 }
770}
771
772/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
773/// signed leb128 value.
774void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000775 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000776 O << "\t.sleb128\t"
777 << Value;
778 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000779 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000780 PrintSLEB128(Value);
781 }
782}
783
784/// EmitInt8 - Emit a byte directive and value.
785///
786void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000787 OutStreamer.EmitIntValue(Value, 1, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000788}
789
790/// EmitInt16 - Emit a short directive and value.
791///
792void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000793 OutStreamer.EmitIntValue(Value, 2, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000794}
795
796/// EmitInt32 - Emit a long directive and value.
797///
798void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000799 OutStreamer.EmitIntValue(Value, 4, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000800}
801
802/// EmitInt64 - Emit a long long directive and value.
803///
804void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner5eaa54e2010-01-20 06:45:39 +0000805 OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000806}
807
808/// toOctal - Convert the low order bits of X into an octal digit.
809///
810static inline char toOctal(int X) {
811 return (X&7)+'0';
812}
813
814/// printStringChar - Print a char, escaped if necessary.
815///
David Greene71847812009-07-14 20:18:05 +0000816static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000817 if (C == '"') {
818 O << "\\\"";
819 } else if (C == '\\') {
820 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000821 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000822 O << C;
823 } else {
824 switch(C) {
825 case '\b': O << "\\b"; break;
826 case '\f': O << "\\f"; break;
827 case '\n': O << "\\n"; break;
828 case '\r': O << "\\r"; break;
829 case '\t': O << "\\t"; break;
830 default:
831 O << '\\';
832 O << toOctal(C >> 6);
833 O << toOctal(C >> 3);
834 O << toOctal(C >> 0);
835 break;
836 }
837 }
838}
839
840/// EmitString - Emit a string with quotes and a null terminator.
841/// Special characters are emitted properly.
842/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000843void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000844 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000845}
846
847void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000848 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000849 if (AscizDirective)
850 O << AscizDirective;
851 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000852 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000853 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000854 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000855 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000856 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000857 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000858 else
859 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000860}
861
862
Dan Gohman189f80d2007-09-24 20:58:13 +0000863/// EmitFile - Emit a .file directive.
Benjamin Kramer1efd4fd52010-01-17 07:46:39 +0000864void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
Dan Gohman189f80d2007-09-24 20:58:13 +0000865 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000866 for (unsigned i = 0, N = Name.size(); i < N; ++i)
867 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000868 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000869}
870
871
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000872//===----------------------------------------------------------------------===//
873
Chris Lattner3a420532007-05-31 18:57:45 +0000874// EmitAlignment - Emit an alignment directive to the specified power of
875// two boundary. For example, if you pass in 3 here, you will get an 8
876// byte alignment. If a global value is specified, and if that global has
877// an explicit alignment requested, it will unconditionally override the
878// alignment request. However, if ForcedAlignBits is specified, this value
879// has final say: the ultimate alignment will be the max of ForcedAlignBits
880// and the alignment computed with NumBits and the global.
881//
882// The algorithm is:
883// Align = NumBits;
884// if (GV && GV->hasalignment) Align = GV->getalignment();
885// Align = std::max(Align, ForcedAlignBits);
886//
887void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000888 unsigned ForcedAlignBits,
889 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000890 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000891 NumBits = Log2_32(GV->getAlignment());
892 NumBits = std::max(NumBits, ForcedAlignBits);
893
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000894 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000895
896 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000897 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000898 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000899
900 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000901}
David Greenea5bb59f2009-08-05 21:00:52 +0000902
Chris Lattnera80ba712004-08-16 23:15:22 +0000903// Print out the specified constant, without a storage class. Only the
904// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000905void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000906 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000907 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000908 return;
909 }
910
911 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000912 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000913 return;
914 }
915
916 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000917 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000918 // name of the variable or function as the address value.
Chris Lattner10b318b2010-01-17 21:43:43 +0000919 O << *GetGlobalValueSymbol(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000920 return;
921 }
922
923 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Chris Lattner10b318b2010-01-17 21:43:43 +0000924 O << *GetBlockAddressSymbol(BA);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000925 return;
926 }
927
928 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
929 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000930 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000931 O << '0';
932 return;
933 }
934
935 switch (CE->getOpcode()) {
936 case Instruction::ZExt:
937 case Instruction::SExt:
938 case Instruction::FPTrunc:
939 case Instruction::FPExt:
940 case Instruction::UIToFP:
941 case Instruction::SIToFP:
942 case Instruction::FPToUI:
943 case Instruction::FPToSI:
944 default:
945 llvm_unreachable("FIXME: Don't support this constant cast expr");
946 case Instruction::GetElementPtr: {
947 // generate a symbolic expression for the byte address
948 const TargetData *TD = TM.getTargetData();
949 const Constant *ptrVal = CE->getOperand(0);
950 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
951 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
952 idxVec.size());
953 if (Offset == 0)
954 return EmitConstantValueOnly(ptrVal);
955
956 // Truncate/sext the offset to the pointer size.
957 if (TD->getPointerSizeInBits() != 64) {
958 int SExtAmount = 64-TD->getPointerSizeInBits();
959 Offset = (Offset << SExtAmount) >> SExtAmount;
960 }
961
962 if (Offset)
963 O << '(';
964 EmitConstantValueOnly(ptrVal);
965 if (Offset > 0)
966 O << ") + " << Offset;
967 else
968 O << ") - " << -Offset;
969 return;
970 }
971 case Instruction::BitCast:
972 return EmitConstantValueOnly(CE->getOperand(0));
973
974 case Instruction::IntToPtr: {
975 // Handle casts to pointers by changing them into casts to the appropriate
976 // integer type. This promotes constant folding and simplifies this code.
977 const TargetData *TD = TM.getTargetData();
978 Constant *Op = CE->getOperand(0);
979 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
980 false/*ZExt*/);
981 return EmitConstantValueOnly(Op);
982 }
983
984 case Instruction::PtrToInt: {
985 // Support only foldable casts to/from pointers that can be eliminated by
986 // changing the pointer to the appropriately sized integer type.
987 Constant *Op = CE->getOperand(0);
988 const Type *Ty = CE->getType();
989 const TargetData *TD = TM.getTargetData();
990
991 // We can emit the pointer value into this slot if the slot is an
992 // integer slot greater or equal to the size of the pointer.
993 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
994 return EmitConstantValueOnly(Op);
995
996 O << "((";
997 EmitConstantValueOnly(Op);
998 APInt ptrMask =
999 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
1000
1001 SmallString<40> S;
1002 ptrMask.toStringUnsigned(S);
1003 O << ") & " << S.str() << ')';
1004 return;
1005 }
1006
1007 case Instruction::Trunc:
1008 // We emit the value and depend on the assembler to truncate the generated
1009 // expression properly. This is important for differences between
1010 // blockaddress labels. Since the two labels are in the same function, it
1011 // is reasonable to treat their delta as a 32-bit value.
1012 return EmitConstantValueOnly(CE->getOperand(0));
1013
1014 case Instruction::Add:
1015 case Instruction::Sub:
1016 case Instruction::And:
1017 case Instruction::Or:
1018 case Instruction::Xor:
1019 O << '(';
1020 EmitConstantValueOnly(CE->getOperand(0));
1021 O << ')';
1022 switch (CE->getOpcode()) {
1023 case Instruction::Add:
1024 O << " + ";
1025 break;
1026 case Instruction::Sub:
1027 O << " - ";
1028 break;
1029 case Instruction::And:
1030 O << " & ";
1031 break;
1032 case Instruction::Or:
1033 O << " | ";
1034 break;
1035 case Instruction::Xor:
1036 O << " ^ ";
1037 break;
1038 default:
1039 break;
1040 }
1041 O << '(';
1042 EmitConstantValueOnly(CE->getOperand(1));
1043 O << ')';
1044 break;
Chris Lattnera80ba712004-08-16 23:15:22 +00001045 }
1046}
Chris Lattner1b7e2352004-08-17 06:36:49 +00001047
Chris Lattner2980cef2005-11-10 18:06:33 +00001048/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +00001049/// the predicate isString is true.
1050///
David Greene71847812009-07-14 20:18:05 +00001051static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +00001052 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +00001053 assert(CVA->isString() && "Array is not string compatible!");
1054
Dan Gohmand19a53b2008-06-30 22:03:41 +00001055 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +00001056 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001057 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +00001058 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +00001059 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001060 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001061 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001062}
1063
Jeff Cohenc884db42006-05-02 01:16:28 +00001064/// EmitString - Emit a zero-byte-terminated string constant.
1065///
1066void AsmPrinter::EmitString(const ConstantArray *CVA) const {
1067 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001068 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +00001069 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001070 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001071 printAsCString(O, CVA, NumElts-1);
1072 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001073 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +00001074 printAsCString(O, CVA, NumElts);
1075 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001076 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +00001077}
1078
Chris Lattner91093ec2010-01-19 19:10:44 +00001079static void EmitGlobalConstantArray(const ConstantArray *CA, unsigned AddrSpace,
1080 AsmPrinter &AP) {
1081 if (AddrSpace == 0 && CA->isString()) {
1082 AP.EmitString(CA);
Dan Gohman00d448a2008-12-22 21:14:27 +00001083 } else { // Not a string. Print the values in successive locations
Chris Lattner91093ec2010-01-19 19:10:44 +00001084 for (unsigned i = 0, e = CA->getNumOperands(); i != e; ++i)
1085 AP.EmitGlobalConstant(CA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001086 }
1087}
1088
Chris Lattner91093ec2010-01-19 19:10:44 +00001089static void EmitGlobalConstantVector(const ConstantVector *CV,
1090 unsigned AddrSpace, AsmPrinter &AP) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001091 for (unsigned i = 0, e = CV->getType()->getNumElements(); i != e; ++i)
Chris Lattner91093ec2010-01-19 19:10:44 +00001092 AP.EmitGlobalConstant(CV->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001093}
1094
Chris Lattner91093ec2010-01-19 19:10:44 +00001095static void EmitGlobalConstantStruct(const ConstantStruct *CS,
1096 unsigned AddrSpace, AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001097 // Print the fields in successive locations. Pad to align if needed!
Chris Lattner91093ec2010-01-19 19:10:44 +00001098 const TargetData *TD = AP.TM.getTargetData();
1099 unsigned Size = TD->getTypeAllocSize(CS->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001100 const StructLayout *Layout = TD->getStructLayout(CS->getType());
Chris Lattner91093ec2010-01-19 19:10:44 +00001101 uint64_t SizeSoFar = 0;
1102 for (unsigned i = 0, e = CS->getNumOperands(); i != e; ++i) {
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001103 const Constant *Field = CS->getOperand(i);
Dan Gohman00d448a2008-12-22 21:14:27 +00001104
1105 // Check if padding is needed and insert one or more 0s.
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001106 uint64_t FieldSize = TD->getTypeAllocSize(Field->getType());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001107 uint64_t PadSize = ((i == e-1 ? Size : Layout->getElementOffset(i+1))
1108 - Layout->getElementOffset(i)) - FieldSize;
1109 SizeSoFar += FieldSize + PadSize;
Dan Gohman00d448a2008-12-22 21:14:27 +00001110
1111 // Now print the actual field value.
Chris Lattnerbcb83e52010-01-20 07:41:15 +00001112 AP.EmitGlobalConstant(Field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001113
1114 // Insert padding - this may include padding to increase the size of the
1115 // current field up to the ABI size (if the struct is not packed) as well
1116 // as padding to ensure that the next field starts at the right offset.
Chris Lattner2dd245c2010-01-20 07:11:32 +00001117 AP.OutStreamer.EmitZeros(PadSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001118 }
Chris Lattner2dd245c2010-01-20 07:11:32 +00001119 assert(SizeSoFar == Layout->getSizeInBytes() &&
Dan Gohman00d448a2008-12-22 21:14:27 +00001120 "Layout of constant struct may be incorrect!");
1121}
1122
Chris Lattner2dd245c2010-01-20 07:11:32 +00001123static void EmitGlobalConstantFP(const ConstantFP *CFP, unsigned AddrSpace,
1124 AsmPrinter &AP) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001125 // FP Constants are printed as integer constants to avoid losing
Chris Lattner9ceff942010-01-20 06:53:37 +00001126 // precision.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001127 if (CFP->getType()->isDoubleTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001128 if (AP.VerboseAsm) {
Chris Lattnerd79d9dc2010-01-22 19:17:48 +00001129 double Val = CFP->getValueAPF().convertToDouble();
1130 AP.OutStreamer.GetCommentOS() << "double " << Val << '\n';
Chris Lattner09ce6742010-01-19 22:16:33 +00001131 }
1132
Chris Lattner2dd245c2010-01-20 07:11:32 +00001133 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1134 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001135 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001136 }
1137
1138 if (CFP->getType()->isFloatTy()) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001139 if (AP.VerboseAsm) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001140 float Val = CFP->getValueAPF().convertToFloat();
1141 AP.OutStreamer.GetCommentOS() << "float " << Val << '\n';
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 }
Chris Lattner2dd245c2010-01-20 07:11:32 +00001143 uint64_t Val = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1144 AP.OutStreamer.EmitIntValue(Val, 4, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001145 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001146 }
1147
1148 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001149 // all long double variants are printed as hex
1150 // api needed to prevent premature destruction
Chris Lattner09ce6742010-01-19 22:16:33 +00001151 APInt API = CFP->getValueAPF().bitcastToAPInt();
1152 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +00001153 if (AP.VerboseAsm) {
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001154 // Convert to double so we can print the approximate val as a comment.
1155 APFloat DoubleVal = CFP->getValueAPF();
1156 bool ignored;
1157 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1158 &ignored);
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001159 AP.OutStreamer.GetCommentOS() << "x86_fp80 ~= "
1160 << DoubleVal.convertToDouble() << '\n';
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001161 }
1162
Chris Lattner2dd245c2010-01-20 07:11:32 +00001163 if (AP.TM.getTargetData()->isBigEndian()) {
1164 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
1165 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner72b5ebc2010-01-19 22:11:05 +00001166 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001167 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1168 AP.OutStreamer.EmitIntValue(p[1], 2, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001169 }
Chris Lattner9ceff942010-01-20 06:53:37 +00001170
1171 // Emit the tail padding for the long double.
Chris Lattner2dd245c2010-01-20 07:11:32 +00001172 const TargetData &TD = *AP.TM.getTargetData();
1173 AP.OutStreamer.EmitZeros(TD.getTypeAllocSize(CFP->getType()) -
1174 TD.getTypeStoreSize(CFP->getType()), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001175 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001176 }
1177
Chris Lattner09ce6742010-01-19 22:16:33 +00001178 assert(CFP->getType()->isPPC_FP128Ty() &&
1179 "Floating point constant type not handled");
1180 // All long double variants are printed as hex api needed to prevent
1181 // premature destruction.
1182 APInt API = CFP->getValueAPF().bitcastToAPInt();
1183 const uint64_t *p = API.getRawData();
Chris Lattner2dd245c2010-01-20 07:11:32 +00001184 if (AP.TM.getTargetData()->isBigEndian()) {
1185 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
1186 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
Chris Lattner9ceff942010-01-20 06:53:37 +00001187 } else {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001188 AP.OutStreamer.EmitIntValue(p[1], 8, AddrSpace);
1189 AP.OutStreamer.EmitIntValue(p[0], 8, AddrSpace);
Chris Lattner09ce6742010-01-19 22:16:33 +00001190 }
Dan Gohman00d448a2008-12-22 21:14:27 +00001191}
1192
Chris Lattner2dd245c2010-01-20 07:11:32 +00001193static void EmitGlobalConstantLargeInt(const ConstantInt *CI,
1194 unsigned AddrSpace, AsmPrinter &AP) {
1195 const TargetData *TD = AP.TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001196 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001197 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001198
1199 // We don't expect assemblers to support integer data directives
1200 // for more than 64 bits, so we emit the data in at most 64-bit
1201 // quantities at a time.
1202 const uint64_t *RawData = CI->getValue().getRawData();
1203 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001204 uint64_t Val = TD->isBigEndian() ? RawData[e - i - 1] : RawData[i];
1205 AP.OutStreamer.EmitIntValue(Val, 8, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001206 }
1207}
1208
Chris Lattner25045bd2005-11-21 07:51:36 +00001209/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001210void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Chris Lattner043c4e52010-01-20 07:19:19 +00001211 if (isa<ConstantAggregateZero>(CV) || isa<UndefValue>(CV)) {
Chris Lattner2dd245c2010-01-20 07:11:32 +00001212 uint64_t Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
Chris Lattner6449abf2010-01-19 21:51:22 +00001213 return OutStreamer.EmitZeros(Size, AddrSpace);
Chris Lattner2dd245c2010-01-20 07:11:32 +00001214 }
1215
1216 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1217 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1218 switch (Size) {
1219 case 1:
1220 case 2:
1221 case 4:
1222 case 8:
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001223 if (VerboseAsm)
1224 OutStreamer.GetCommentOS() << format("0x%llx\n", CI->getZExtValue());
Chris Lattner2dd245c2010-01-20 07:11:32 +00001225 OutStreamer.EmitIntValue(CI->getZExtValue(), Size, AddrSpace);
1226 return;
1227 default:
1228 EmitGlobalConstantLargeInt(CI, AddrSpace, *this);
1229 return;
1230 }
1231 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001232
Chris Lattner91093ec2010-01-19 19:10:44 +00001233 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV))
1234 return EmitGlobalConstantArray(CVA, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001235
Chris Lattner91093ec2010-01-19 19:10:44 +00001236 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV))
1237 return EmitGlobalConstantStruct(CVS, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001238
Chris Lattner91093ec2010-01-19 19:10:44 +00001239 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV))
Chris Lattner2dd245c2010-01-20 07:11:32 +00001240 return EmitGlobalConstantFP(CFP, AddrSpace, *this);
Chris Lattner3cc3a002010-01-13 04:34:19 +00001241
Chris Lattner91093ec2010-01-19 19:10:44 +00001242 if (const ConstantVector *V = dyn_cast<ConstantVector>(CV))
1243 return EmitGlobalConstantVector(V, AddrSpace, *this);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001244
Chris Lattnerb0bedd62010-01-20 17:57:50 +00001245 if (isa<ConstantPointerNull>(CV)) {
1246 unsigned Size = TM.getTargetData()->getTypeAllocSize(CV->getType());
1247 OutStreamer.EmitIntValue(0, Size, AddrSpace);
1248 return;
1249 }
1250
Chris Lattnerea3cb402010-01-20 07:33:29 +00001251 // Otherwise, it must be a ConstantExpr. Emit the data directive, then emit
1252 // the expression value.
1253 switch (TM.getTargetData()->getTypeAllocSize(CV->getType())) {
1254 case 0: return;
1255 case 1: O << MAI->getData8bitsDirective(AddrSpace); break;
1256 case 2: O << MAI->getData16bitsDirective(AddrSpace); break;
1257 case 4: O << MAI->getData32bitsDirective(AddrSpace); break;
1258 case 8:
1259 if (const char *Dir = MAI->getData64bitsDirective(AddrSpace)) {
1260 O << Dir;
1261 break;
1262 }
1263 // FALL THROUGH.
1264 default:
1265 llvm_unreachable("Target cannot handle given data directive width!");
1266 return;
1267 }
1268
Chris Lattner25045bd2005-11-21 07:51:36 +00001269 EmitConstantValueOnly(CV);
Dan Gohmand19a53b2008-06-30 22:03:41 +00001270 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001271}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001272
Chris Lattnerfad86b02008-08-17 07:19:36 +00001273void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001274 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001275 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001276}
1277
Chris Lattner3ce9b672006-09-26 23:59:50 +00001278/// PrintSpecial - Print information related to the specified machine instr
1279/// that is independent of the operand, and may be independent of the instr
1280/// itself. This can be useful for portably encoding the comment character
1281/// or other bits of target-specific knowledge into the asmstrings. The
1282/// syntax used is ${:comment}. Targets can override this to add support
1283/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001284void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001285 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001286 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001287 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001288 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001289 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001290 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001291 // Comparing the address of MI isn't sufficient, because machineinstrs may
1292 // be allocated to the same address across functions.
1293 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1294
Owen Andersonbd58edf2009-06-24 22:28:12 +00001295 // If this is a new LastFn instruction, bump the counter.
1296 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001297 ++Counter;
1298 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001299 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001300 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001301 O << Counter;
1302 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001303 std::string msg;
1304 raw_string_ostream Msg(msg);
1305 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001306 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001307 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001308 }
1309}
1310
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001311/// processDebugLoc - Processes the debug information of each machine
1312/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001313void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1314 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001315 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1316 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001317 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001318 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001319 if (DL.isUnknown())
1320 return;
Devang Patel6b61f582010-01-16 06:09:35 +00001321 DILocation CurDLT = MF->getDILocation(DL);
1322 if (CurDLT.getScope().isNull())
Devang Patel53bb5c92009-11-10 23:06:00 +00001323 return;
1324
Chris Lattner043c4e52010-01-20 07:19:19 +00001325 if (!BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001326 // After printing instruction
1327 DW->EndScope(MI);
Chris Lattner043c4e52010-01-20 07:19:19 +00001328 } else if (CurDLT.getNode() != PrevDLT) {
1329 unsigned L = DW->RecordSourceLine(CurDLT.getLineNumber(),
1330 CurDLT.getColumnNumber(),
1331 CurDLT.getScope().getNode());
1332 printLabel(L);
1333 O << '\n';
1334 DW->BeginScope(MI, L);
1335 PrevDLT = CurDLT.getNode();
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001336 }
1337}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001338
Devang Patel53bb5c92009-11-10 23:06:00 +00001339
Chris Lattner0264d1a2006-01-27 02:10:10 +00001340/// printInlineAsm - This method formats and prints the specified machine
1341/// instruction that is an inline asm.
1342void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001343 unsigned NumOperands = MI->getNumOperands();
1344
1345 // Count the number of register definitions.
1346 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001347 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001348 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001349 assert(NumDefs != NumOperands-1 && "No asm string?");
1350
Dan Gohmand735b802008-10-03 15:45:36 +00001351 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001352
1353 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001354 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001355
Dan Gohman40c57862009-11-06 00:04:54 +00001356 O << '\t';
1357
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001358 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1359 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001360 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001361 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1362 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001363 return;
1364 }
1365
Chris Lattner33adcfb2009-08-22 21:43:10 +00001366 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001367
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001368 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001369 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001370
Chris Lattner66099132006-02-01 22:41:11 +00001371 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1372 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001373
Chris Lattner66099132006-02-01 22:41:11 +00001374 while (*LastEmitted) {
1375 switch (*LastEmitted) {
1376 default: {
1377 // Not a special case, emit the string section literally.
1378 const char *LiteralEnd = LastEmitted+1;
1379 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001380 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001381 ++LiteralEnd;
1382 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1383 O.write(LastEmitted, LiteralEnd-LastEmitted);
1384 LastEmitted = LiteralEnd;
1385 break;
1386 }
Chris Lattner1c059972006-05-05 21:47:05 +00001387 case '\n':
1388 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001389 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001390 break;
Chris Lattner66099132006-02-01 22:41:11 +00001391 case '$': {
1392 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001393 bool Done = true;
1394
1395 // Handle escapes.
1396 switch (*LastEmitted) {
1397 default: Done = false; break;
1398 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001399 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1400 O << '$';
1401 ++LastEmitted; // Consume second '$' character.
1402 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001403 case '(': // $( -> same as GCC's { character.
1404 ++LastEmitted; // Consume '(' character.
1405 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001406 llvm_report_error("Nested variants found in inline asm string: '"
1407 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001408 }
1409 CurVariant = 0; // We're in the first variant now.
1410 break;
1411 case '|':
1412 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001413 if (CurVariant == -1)
1414 O << '|'; // this is gcc's behavior for | outside a variant
1415 else
1416 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001417 break;
1418 case ')': // $) -> same as GCC's } char.
1419 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001420 if (CurVariant == -1)
1421 O << '}'; // this is gcc's behavior for } outside a variant
1422 else
1423 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001424 break;
Chris Lattner66099132006-02-01 22:41:11 +00001425 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001426 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001427
1428 bool HasCurlyBraces = false;
1429 if (*LastEmitted == '{') { // ${variable}
1430 ++LastEmitted; // Consume '{' character.
1431 HasCurlyBraces = true;
1432 }
1433
Chris Lattner3e0cc262009-03-10 05:37:13 +00001434 // If we have ${:foo}, then this is not a real operand reference, it is a
1435 // "magic" string reference, just like in .td files. Arrange to call
1436 // PrintSpecial.
1437 if (HasCurlyBraces && *LastEmitted == ':') {
1438 ++LastEmitted;
1439 const char *StrStart = LastEmitted;
1440 const char *StrEnd = strchr(StrStart, '}');
1441 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001442 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1443 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001444 }
1445
1446 std::string Val(StrStart, StrEnd);
1447 PrintSpecial(MI, Val.c_str());
1448 LastEmitted = StrEnd+1;
1449 break;
1450 }
1451
Chris Lattner66099132006-02-01 22:41:11 +00001452 const char *IDStart = LastEmitted;
1453 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001454 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001455 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1456 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001457 llvm_report_error("Bad $ operand number in inline asm string: '"
1458 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001459 }
1460 LastEmitted = IDEnd;
1461
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001462 char Modifier[2] = { 0, 0 };
1463
Chris Lattner66099132006-02-01 22:41:11 +00001464 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001465 // If we have curly braces, check for a modifier character. This
1466 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1467 if (*LastEmitted == ':') {
1468 ++LastEmitted; // Consume ':' character.
1469 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001470 llvm_report_error("Bad ${:} expression in inline asm string: '"
1471 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001472 }
1473
1474 Modifier[0] = *LastEmitted;
1475 ++LastEmitted; // Consume modifier character.
1476 }
1477
Chris Lattner66099132006-02-01 22:41:11 +00001478 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001479 llvm_report_error("Bad ${} expression in inline asm string: '"
1480 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001481 }
1482 ++LastEmitted; // Consume '}' character.
1483 }
1484
1485 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001486 llvm_report_error("Invalid $ operand number in inline asm string: '"
1487 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001488 }
1489
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001490 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001491 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001492 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1493 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001494
1495 bool Error = false;
1496
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001497 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001498 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001499 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001500 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001501 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001502 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001503
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001504 if (OpNo >= MI->getNumOperands()) {
1505 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001506 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001507 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001508 ++OpNo; // Skip over the ID number.
1509
Chris Lattner10b318b2010-01-17 21:43:43 +00001510 if (Modifier[0] == 'l') // labels are target independent
1511 O << *GetMBBSymbol(MI->getOperand(OpNo).getMBB()->getNumber());
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001512 else {
1513 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001514 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001515 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1516 Modifier[0] ? Modifier : 0);
1517 } else {
1518 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1519 Modifier[0] ? Modifier : 0);
1520 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001521 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001522 }
1523 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001524 std::string msg;
1525 raw_string_ostream Msg(msg);
Chris Lattner10b318b2010-01-17 21:43:43 +00001526 Msg << "Invalid operand found in inline asm: '" << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001527 MI->print(Msg);
1528 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001529 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001530 }
Chris Lattner66099132006-02-01 22:41:11 +00001531 break;
1532 }
Chris Lattner66099132006-02-01 22:41:11 +00001533 }
1534 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001535 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001536}
1537
Evan Chengda47e6e2008-03-15 00:03:38 +00001538/// printImplicitDef - This method prints the specified machine instruction
1539/// that is an implicit def.
1540void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001541 if (!VerboseAsm) return;
1542 O.PadToColumn(MAI->getCommentColumn());
1543 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001544 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001545}
1546
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001547void AsmPrinter::printKill(const MachineInstr *MI) const {
1548 if (!VerboseAsm) return;
1549 O.PadToColumn(MAI->getCommentColumn());
1550 O << MAI->getCommentString() << " kill:";
1551 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1552 const MachineOperand &op = MI->getOperand(n);
1553 assert(op.isReg() && "KILL instruction must have only register operands");
1554 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1555 }
1556}
1557
Jim Laskey1ee29252007-01-26 14:34:52 +00001558/// printLabel - This method prints a local label used by debug and
1559/// exception handling tables.
1560void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001561 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001562}
1563
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001564void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001565 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001566}
1567
Chris Lattner66099132006-02-01 22:41:11 +00001568/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1569/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001570/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001571bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001572 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001573 // Target doesn't support this yet!
1574 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001575}
Chris Lattnerdd260332006-02-24 20:21:58 +00001576
1577bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1578 unsigned AsmVariant,
1579 const char *ExtraCode) {
1580 // Target doesn't support this yet!
1581 return true;
1582}
Nate Begeman37efe672006-04-22 18:53:45 +00001583
Dan Gohman29cbade2009-11-20 23:18:13 +00001584MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1585 const char *Suffix) const {
1586 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001587}
1588
1589MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001590 const BasicBlock *BB,
1591 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001592 assert(BB->hasName() &&
1593 "Address of anonymous basic block not supported yet!");
1594
Dan Gohman568a3be2009-11-05 23:14:35 +00001595 // This code must use the function name itself, and not the function number,
1596 // since it must be possible to generate the label name from within other
1597 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001598 SmallString<60> FnName;
1599 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001600
Chris Lattner2f8cc262010-01-13 07:30:49 +00001601 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001602 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001603 Mang->getNameWithPrefix(NameResult,
1604 StringRef("BA") + Twine((unsigned)FnName.size()) +
1605 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1606 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001607
Chris Lattner48130352010-01-13 06:38:18 +00001608 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001609}
1610
Chris Lattner7cb384d2009-09-12 23:02:08 +00001611MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1612 SmallString<60> Name;
1613 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1614 << getFunctionNumber() << '_' << MBBID;
1615
1616 return OutContext.GetOrCreateSymbol(Name.str());
1617}
1618
Chris Lattner6b04ede2010-01-15 23:18:17 +00001619/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1620/// value.
1621MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1622 SmallString<60> NameStr;
1623 Mang->getNameWithPrefix(NameStr, GV, false);
1624 return OutContext.GetOrCreateSymbol(NameStr.str());
1625}
1626
Chris Lattner7a2ba942010-01-16 18:37:32 +00001627/// GetSymbolWithGlobalValueBase - Return the MCSymbol for a symbol with
Chris Lattnerd588b972010-01-15 23:25:11 +00001628/// global value name as its base, with the specified suffix, and where the
Chris Lattner7a2ba942010-01-16 18:37:32 +00001629/// symbol is forced to have private linkage if ForcePrivate is true.
1630MCSymbol *AsmPrinter::GetSymbolWithGlobalValueBase(const GlobalValue *GV,
1631 StringRef Suffix,
1632 bool ForcePrivate) const {
Chris Lattnerd588b972010-01-15 23:25:11 +00001633 SmallString<60> NameStr;
Chris Lattner7a2ba942010-01-16 18:37:32 +00001634 Mang->getNameWithPrefix(NameStr, GV, ForcePrivate);
Chris Lattnerd588b972010-01-15 23:25:11 +00001635 NameStr.append(Suffix.begin(), Suffix.end());
1636 return OutContext.GetOrCreateSymbol(NameStr.str());
1637}
1638
Chris Lattner6b04ede2010-01-15 23:18:17 +00001639/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1640/// ExternalSymbol.
1641MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1642 SmallString<60> NameStr;
1643 Mang->getNameWithPrefix(NameStr, Sym);
1644 return OutContext.GetOrCreateSymbol(NameStr.str());
1645}
1646
Chris Lattner7cb384d2009-09-12 23:02:08 +00001647
Chris Lattner523a5082010-01-22 21:50:41 +00001648
1649/// PrintParentLoopComment - Print comments about parent loops of this one.
1650static void PrintParentLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1651 unsigned FunctionNumber) {
1652 if (Loop == 0) return;
1653 PrintParentLoopComment(OS, Loop->getParentLoop(), FunctionNumber);
1654 OS.indent(Loop->getLoopDepth()*2)
1655 << "Parent Loop BB" << FunctionNumber << "_"
1656 << Loop->getHeader()->getNumber()
1657 << " Depth=" << Loop->getLoopDepth() << '\n';
1658}
1659
1660
1661/// PrintChildLoopComment - Print comments about child loops within
1662/// the loop for this basic block, with nesting.
1663static void PrintChildLoopComment(raw_ostream &OS, const MachineLoop *Loop,
1664 unsigned FunctionNumber) {
1665 // Add child loop information
1666 for (MachineLoop::iterator CL = Loop->begin(), E = Loop->end();CL != E; ++CL){
1667 OS.indent((*CL)->getLoopDepth()*2)
1668 << "Child Loop BB" << FunctionNumber << "_"
1669 << (*CL)->getHeader()->getNumber() << " Depth " << (*CL)->getLoopDepth()
1670 << '\n';
1671 PrintChildLoopComment(OS, *CL, FunctionNumber);
1672 }
1673}
1674
1675/// EmitComments - Pretty-print comments for basic blocks.
1676static void PrintBasicBlockLoopComments(const MachineBasicBlock &MBB,
1677 const MachineLoopInfo *LI,
1678 const AsmPrinter &AP) {
1679 // Add loop depth information
1680 const MachineLoop *Loop = LI->getLoopFor(&MBB);
1681 if (Loop == 0) return;
1682
1683 MachineBasicBlock *Header = Loop->getHeader();
1684 assert(Header && "No header for loop");
1685
1686 // If this block is not a loop header, just print out what is the loop header
1687 // and return.
1688 if (Header != &MBB) {
1689 AP.OutStreamer.AddComment(" in Loop: Header=BB" +
1690 Twine(AP.getFunctionNumber())+"_" +
1691 Twine(Loop->getHeader()->getNumber())+
1692 " Depth="+Twine(Loop->getLoopDepth()));
1693 return;
1694 }
1695
1696 // Otherwise, it is a loop header. Print out information about child and
1697 // parent loops.
1698 raw_ostream &OS = AP.OutStreamer.GetCommentOS();
1699
1700 PrintParentLoopComment(OS, Loop->getParentLoop(), AP.getFunctionNumber());
1701
1702 OS << "=>";
1703 OS.indent(Loop->getLoopDepth()*2-2);
1704
1705 OS << "This ";
1706 if (Loop->empty())
1707 OS << "Inner ";
1708 OS << "Loop Header: Depth=" + Twine(Loop->getLoopDepth()) << '\n';
1709
1710 PrintChildLoopComment(OS, Loop, AP.getFunctionNumber());
1711}
1712
1713
Chris Lattner70a54c02009-09-13 18:25:37 +00001714/// EmitBasicBlockStart - This method prints the label for the specified
1715/// MachineBasicBlock, an alignment (if present) and a comment describing
1716/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001717void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001718 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001719 if (unsigned Align = MBB->getAlignment())
1720 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001721
Dan Gohmanb1cac332009-10-30 01:34:35 +00001722 // If the block has its address taken, emit a special label to satisfy
1723 // references to the block. This is done so that we don't need to
1724 // remember the number of this label, and so that we can make
1725 // forward references to labels without knowing what their numbers
1726 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001727 if (MBB->hasAddressTaken()) {
Chris Lattner213168b2010-01-20 07:24:05 +00001728 const BasicBlock *BB = MBB->getBasicBlock();
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001729 if (VerboseAsm)
1730 OutStreamer.AddComment("Address Taken");
Chris Lattner213168b2010-01-20 07:24:05 +00001731 OutStreamer.EmitLabel(GetBlockAddressSymbol(BB->getParent(), BB));
Dan Gohman8c2b5252009-10-30 01:27:03 +00001732 }
1733
Dan Gohmanb1cac332009-10-30 01:34:35 +00001734 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001735 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001736 if (VerboseAsm) {
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001737 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001738 if (const BasicBlock *BB = MBB->getBasicBlock())
1739 if (BB->hasName())
1740 OutStreamer.AddComment("%" + BB->getName());
Chris Lattnerd8d0aee2010-01-22 21:00:45 +00001741
Chris Lattner523a5082010-01-22 21:50:41 +00001742 PrintBasicBlockLoopComments(*MBB, LI, *this);
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001743 OutStreamer.AddBlankLine();
1744 }
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001745 } else {
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001746 if (VerboseAsm) {
1747 if (const BasicBlock *BB = MBB->getBasicBlock())
1748 if (BB->hasName())
1749 OutStreamer.AddComment("%" + BB->getName());
Chris Lattner523a5082010-01-22 21:50:41 +00001750 PrintBasicBlockLoopComments(*MBB, LI, *this);
Chris Lattner0fd90fd2010-01-22 19:52:01 +00001751 }
Chris Lattnerd8d0aee2010-01-22 21:00:45 +00001752
Chris Lattner213168b2010-01-20 07:24:05 +00001753 OutStreamer.EmitLabel(GetMBBSymbol(MBB->getNumber()));
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001754 }
Nate Begeman37efe672006-04-22 18:53:45 +00001755}
Nate Begeman52a51e382006-08-12 21:29:52 +00001756
Evan Chengcc415862007-11-09 01:32:10 +00001757/// printPICJumpTableSetLabel - This method prints a set label for the
1758/// specified MachineBasicBlock for a jumptable entry.
1759void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1760 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001761 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001762 return;
1763
Chris Lattner33adcfb2009-08-22 21:43:10 +00001764 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Chris Lattner10b318b2010-01-17 21:43:43 +00001765 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
1766 << *GetMBBSymbol(MBB->getNumber())
1767 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001768 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001769}
Evan Chengd6594ae2006-09-12 21:00:35 +00001770
Evan Chengcc415862007-11-09 01:32:10 +00001771void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1772 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001773 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001774 return;
1775
Chris Lattner33adcfb2009-08-22 21:43:10 +00001776 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001777 << getFunctionNumber() << '_' << uid << '_' << uid2
Chris Lattner10b318b2010-01-17 21:43:43 +00001778 << "_set_" << MBB->getNumber() << ','
1779 << *GetMBBSymbol(MBB->getNumber())
1780 << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001781 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001782}
1783
Chris Lattner53d4d782010-01-15 23:38:51 +00001784void AsmPrinter::printVisibility(const MCSymbol *Sym,
1785 unsigned Visibility) const {
1786 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001787 if (const char *Directive = MAI->getHiddenDirective())
1788 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001789 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner10b318b2010-01-17 21:43:43 +00001790 if (const char *Directive = MAI->getProtectedDirective())
1791 O << Directive << *Sym << '\n';
Chris Lattner53d4d782010-01-15 23:38:51 +00001792 }
1793}
1794
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001795void AsmPrinter::printOffset(int64_t Offset) const {
1796 if (Offset > 0)
1797 O << '+' << Offset;
1798 else if (Offset < 0)
1799 O << Offset;
1800}
1801
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001802GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1803 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001804 return 0;
1805
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001806 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001807 if (GCPI != GCMetadataPrinters.end())
1808 return GCPI->second;
1809
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001810 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001811
1812 for (GCMetadataPrinterRegistry::iterator
1813 I = GCMetadataPrinterRegistry::begin(),
1814 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1815 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001816 GCMetadataPrinter *GMP = I->instantiate();
1817 GMP->S = S;
1818 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1819 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001820 }
1821
Chris Lattner103289e2009-08-23 07:19:13 +00001822 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001823 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001824}
David Greene014700c2009-07-13 20:25:48 +00001825
1826/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001827void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001828 if (!VerboseAsm)
1829 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001830
David Greene1924aab2009-11-13 21:34:57 +00001831 bool Newline = false;
1832
1833 if (!MI.getDebugLoc().isUnknown()) {
Devang Patel6b61f582010-01-16 06:09:35 +00001834 DILocation DLT = MF->getDILocation(MI.getDebugLoc());
David Greene1924aab2009-11-13 21:34:57 +00001835
1836 // Print source line info.
1837 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001838 O << MAI->getCommentString() << ' ';
Devang Patel6b61f582010-01-16 06:09:35 +00001839 DIScope Scope = DLT.getScope();
Dan Gohman3b9bc042009-12-05 00:23:29 +00001840 // Omit the directory, because it's likely to be long and uninteresting.
1841 if (!Scope.isNull())
1842 O << Scope.getFilename();
1843 else
1844 O << "<unknown>";
Devang Patel6b61f582010-01-16 06:09:35 +00001845 O << ':' << DLT.getLineNumber();
1846 if (DLT.getColumnNumber() != 0)
1847 O << ':' << DLT.getColumnNumber();
David Greene1924aab2009-11-13 21:34:57 +00001848 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001849 }
David Greene1924aab2009-11-13 21:34:57 +00001850
David Greeneddff9412009-11-16 15:12:23 +00001851 // Check for spills and reloads
1852 int FI;
1853
1854 const MachineFrameInfo *FrameInfo =
1855 MI.getParent()->getParent()->getFrameInfo();
1856
1857 // We assume a single instruction only has a spill or reload, not
1858 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001859 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001860 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1861 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001862 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001863 if (Newline) O << '\n';
1864 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001865 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001866 Newline = true;
1867 }
1868 }
David Greenef7ea2a52009-12-04 22:46:04 +00001869 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001870 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1871 if (Newline) O << '\n';
1872 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001873 O << MAI->getCommentString() << ' '
1874 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001875 Newline = true;
1876 }
1877 }
1878 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1879 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001880 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001881 if (Newline) O << '\n';
1882 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001883 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001884 Newline = true;
1885 }
1886 }
David Greenef7ea2a52009-12-04 22:46:04 +00001887 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001888 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1889 if (Newline) O << '\n';
1890 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001891 O << MAI->getCommentString() << ' '
1892 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001893 Newline = true;
1894 }
1895 }
1896
1897 // Check for spill-induced copies
1898 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1899 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1900 SrcSubIdx, DstSubIdx)) {
1901 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1902 if (Newline) O << '\n';
1903 O.PadToColumn(MAI->getCommentColumn());
1904 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001905 }
1906 }
David Greene014700c2009-07-13 20:25:48 +00001907}
1908