blob: 577d173ff13ba2677748781428edbb305f40d0a5 [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"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greene1924aab2009-11-13 21:34:57 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
David Greenefe37ab32009-08-18 19:22:55 +000022#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000023#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000024#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000026#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000027#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000028#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000029#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000030#include "llvm/MC/MCSection.h"
31#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000032#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000033#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000034#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000035#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000036#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000037#include "llvm/MC/MCAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000038#include "llvm/Target/TargetData.h"
David Greene1924aab2009-11-13 21:34:57 +000039#include "llvm/Target/TargetInstrInfo.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000040#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000041#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000042#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000043#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000044#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000045#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000046#include "llvm/ADT/StringExtras.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
Devang Patel19974732007-05-03 01:11:54 +000054char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000055AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000056 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000057 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000058 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000059
60 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000061 // FIXME: Pass instprinter to streamer.
62 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000063
Chris Lattner290c2f52009-08-03 23:20:21 +000064 LastMI(0), LastFn(0), Counter(~0U),
Devang Patela1434042009-10-01 01:15:28 +000065 PrevDLT(0, 0, ~0U, ~0U) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000066 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000067 switch (AsmVerbose) {
68 case cl::BOU_UNSET: VerboseAsm = VDef; break;
69 case cl::BOU_TRUE: VerboseAsm = true; break;
70 case cl::BOU_FALSE: VerboseAsm = false; break;
71 }
72}
Chris Lattnered138932005-12-13 06:32:10 +000073
Gordon Henriksenc317a602008-08-17 12:08:44 +000074AsmPrinter::~AsmPrinter() {
75 for (gcp_iterator I = GCMetadataPrinters.begin(),
76 E = GCMetadataPrinters.end(); I != E; ++I)
77 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000078
79 delete &OutStreamer;
80 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000081}
Chris Lattnered138932005-12-13 06:32:10 +000082
Chris Lattner38c39882009-08-03 19:12:26 +000083TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000084 return TM.getTargetLowering()->getObjFileLowering();
85}
86
Chris Lattnerdabf07c2009-08-18 06:15:16 +000087/// getCurrentSection() - Return the current section we are emitting to.
88const MCSection *AsmPrinter::getCurrentSection() const {
89 return OutStreamer.getCurrentSection();
90}
91
92
Gordon Henriksence224772008-01-07 01:30:38 +000093void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000094 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000095 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000096 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000097 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000098 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000099}
100
Chris Lattnera80ba712004-08-16 23:15:22 +0000101bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000102 // Initialize TargetLoweringObjectFile.
103 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
104 .Initialize(OutContext, TM);
105
Chris Lattner33adcfb2009-08-22 21:43:10 +0000106 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
107 MAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000108
Chris Lattner33adcfb2009-08-22 21:43:10 +0000109 if (MAI->doesAllowQuotesInName())
Chris Lattnera93ca922009-06-18 23:41:35 +0000110 Mang->setUseQuotes(true);
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +0000111
112 if (MAI->doesAllowNameToStartWithDigit())
113 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattnera93ca922009-06-18 23:41:35 +0000114
Bob Wilson812209a2009-09-30 22:06:26 +0000115 // Allow the target to emit any magic that it wants at the start of the file.
116 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000117
Chris Lattner33adcfb2009-08-22 21:43:10 +0000118 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000119 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000120 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000121 a function came from. */
122 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
123 }
124
Bob Wilson812209a2009-09-30 22:06:26 +0000125 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
126 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000127 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
128 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000129 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000130
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000131 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000132 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000133 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000134 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000135 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000136
Chris Lattnerb55e0682009-09-24 05:44:53 +0000137 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
138 if (MMI)
139 MMI->AnalyzeModule(M);
140 DW = getAnalysisIfAvailable<DwarfWriter>();
Sanjiv Gupta9a501cf2009-11-14 07:22:25 +0000141 if (DW)
Chris Lattnerb55e0682009-09-24 05:44:53 +0000142 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000143
Chris Lattnera80ba712004-08-16 23:15:22 +0000144 return false;
145}
146
147bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000148 // Emit global variables.
149 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
150 I != E; ++I)
151 PrintGlobalVariable(I);
152
Chris Lattner1f522fe2009-06-24 18:54:37 +0000153 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000154 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000155 DW->EndModule();
156
Chris Lattner0a7befa2009-06-24 18:52:01 +0000157 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000158 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000159 // FIXME: This is not lazy, it would be nice to only print weak references
160 // to stuff that is actually used. Note that doing so would require targets
161 // to notice uses in operands (due to constant exprs etc). This should
162 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000163
Chris Lattner0a7befa2009-06-24 18:52:01 +0000164 // Print out module-level global variables here.
165 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
166 I != E; ++I) {
167 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000168 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000169 }
170
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000171 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000172 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000173 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000174 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000175 }
176
Chris Lattner33adcfb2009-08-22 21:43:10 +0000177 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000178 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000179 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000180 I != E; ++I) {
Chris Lattner5c40e692010-01-16 01:17:26 +0000181 MCSymbol *Name = GetGlobalValueSymbol(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000182
183 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattner5c40e692010-01-16 01:17:26 +0000184 MCSymbol *Target = GetGlobalValueSymbol(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000185
Chris Lattner5c40e692010-01-16 01:17:26 +0000186 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective()) {
187 O << "\t.globl\t";
188 Name->print(O, MAI);
189 O << '\n';
190 } else if (I->hasWeakLinkage()) {
191 O << MAI->getWeakRefDirective();
192 Name->print(O, MAI);
193 O << '\n';
194 } else {
195 assert(!I->hasLocalLinkage() && "Invalid alias linkage");
196 }
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000197
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000198 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000199
Chris Lattner5c40e692010-01-16 01:17:26 +0000200 O << MAI->getSetDirective() << ' ';
201 Name->print(O, MAI);
202 O << ", ";
203 Target->print(O, MAI);
204 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000205 }
206 }
207
Duncan Sands1465d612009-01-28 13:14:17 +0000208 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000209 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
210 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
211 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000212 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000213
Dan Gohmana779a982008-05-05 00:28:39 +0000214 // If we don't have any trampolines, then we don't require stack memory
215 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000216 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000217 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000218 if (MAI->getNonexecutableStackDirective())
219 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000220
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000221
222 // Allow the target to emit any magic that it wants at the end of the file,
223 // after everything else has gone out.
224 EmitEndOfAsmFile(M);
225
Chris Lattnera80ba712004-08-16 23:15:22 +0000226 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000227 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000228
229 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000230 return false;
231}
232
Chris Lattner25045bd2005-11-21 07:51:36 +0000233void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000234 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000235 CurrentFnName = Mang->getMangledName(MF.getFunction());
Chris Lattnerd1947ed2010-01-15 23:55:16 +0000236 CurrentFnSym = GetGlobalValueSymbol(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000237 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000238
Chris Lattner25d812b2009-09-16 00:35:39 +0000239 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000240 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000241}
242
Evan Cheng1606e8e2009-03-13 07:51:59 +0000243namespace {
244 // SectionCPs - Keep track the alignment, constpool entries per Section.
245 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000246 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000247 unsigned Alignment;
248 SmallVector<unsigned, 4> CPEs;
Douglas Gregorcabdd742009-12-19 07:05:23 +0000249 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {}
Evan Cheng1606e8e2009-03-13 07:51:59 +0000250 };
251}
252
Chris Lattner3b4fd322005-11-21 08:25:09 +0000253/// EmitConstantPool - Print to the current output stream assembly
254/// representations of the constants in the constant pool MCP. This is
255/// used to print out constants which have been "spilled to memory" by
256/// the code generator.
257///
258void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000259 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000260 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000261
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000262 // Calculate sections for constant pool entries. We collect entries to go into
263 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000264 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000265 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000266 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000267 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000268
269 SectionKind Kind;
270 switch (CPE.getRelocationInfo()) {
271 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000272 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000273 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000274 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000275 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000276 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000277 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000278 case 4: Kind = SectionKind::getMergeableConst4(); break;
279 case 8: Kind = SectionKind::getMergeableConst8(); break;
280 case 16: Kind = SectionKind::getMergeableConst16();break;
281 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000282 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000283 }
284
Chris Lattner83d77fa2009-08-01 23:46:12 +0000285 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000286
Evan Cheng1606e8e2009-03-13 07:51:59 +0000287 // The number of sections are small, just do a linear search from the
288 // last section to the first.
289 bool Found = false;
290 unsigned SecIdx = CPSections.size();
291 while (SecIdx != 0) {
292 if (CPSections[--SecIdx].S == S) {
293 Found = true;
294 break;
295 }
296 }
297 if (!Found) {
298 SecIdx = CPSections.size();
299 CPSections.push_back(SectionCPs(S, Align));
300 }
301
302 if (Align > CPSections[SecIdx].Alignment)
303 CPSections[SecIdx].Alignment = Align;
304 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000305 }
306
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000307 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000308 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000309 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000310 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000311
Evan Cheng1606e8e2009-03-13 07:51:59 +0000312 unsigned Offset = 0;
313 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
314 unsigned CPI = CPSections[i].CPEs[j];
315 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000316
Chris Lattner3029f922006-02-09 04:46:04 +0000317 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000318 unsigned AlignMask = CPE.getAlignment() - 1;
319 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
320 EmitZeros(NewOffset - Offset);
321
322 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000323 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000324
Chris Lattner33adcfb2009-08-22 21:43:10 +0000325 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000326 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000327 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000328 O.PadToColumn(MAI->getCommentColumn());
329 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000330 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000331 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000332 O << '\n';
333 if (CPE.isMachineConstantPoolEntry())
334 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
335 else
336 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000337 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000338 }
339}
340
Nate Begeman37efe672006-04-22 18:53:45 +0000341/// EmitJumpTableInfo - Print assembly representations of the jump tables used
342/// by the current function to the current output stream.
343///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000344void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
345 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000346 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
347 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000348
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000349 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000350
Nate Begeman2f1ae882006-07-27 01:13:04 +0000351 // Pick the directive to use to print the jump table entries, and switch to
352 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000353 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000354
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000355 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000356 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000357 if (F->isWeakForLinker() ||
358 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000359 // In PIC mode, we need to emit the jump table to the same section as the
360 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000361 // We should also do if the section name is NULL or function is declared in
362 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000363 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
364 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000365 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000366 // Otherwise, drop it in the readonly section.
367 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000368 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000369 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000370 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000371 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000372
373 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000374
Nate Begeman37efe672006-04-22 18:53:45 +0000375 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000376 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000377
378 // If this jump table was deleted, ignore it.
379 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000380
381 // For PIC codegen, if possible we want to use the SetDirective to reduce
382 // the number of relocations the assembler will generate for the jump table.
383 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000384 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000385 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000386 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000387 if (EmittedSets.insert(JTBBs[ii]))
388 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000389
Chris Lattner7c301912009-09-13 19:02:16 +0000390 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000391 // before each jump table. The first label is never referenced, but tells
392 // the assembler and linker the extents of the jump table object. The
393 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000394 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
395 O << MAI->getLinkerPrivateGlobalPrefix()
396 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000397 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000398
Chris Lattner33adcfb2009-08-22 21:43:10 +0000399 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000400 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000401
Nate Begeman37efe672006-04-22 18:53:45 +0000402 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000403 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000404 O << '\n';
405 }
406 }
407}
408
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000409void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
410 const MachineBasicBlock *MBB,
411 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000412 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000413
414 // Use JumpTableDirective otherwise honor the entry size from the jump table
415 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000416 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000417 bool HadJTEntryDirective = JTEntryDirective != NULL;
418 if (!HadJTEntryDirective) {
419 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000420 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000421 }
422
423 O << JTEntryDirective << ' ';
424
425 // If we have emitted set directives for the jump table entries, print
426 // them rather than the entries themselves. If we're emitting PIC, then
427 // emit the table entries as differences between two text section labels.
428 // If we're emitting non-PIC code, then emit the entries as direct
429 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000430 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000431 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000432 } else if (MAI->getSetDirective()) {
433 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000434 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000435 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000436 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000437 // If the arch uses custom Jump Table directives, don't calc relative to
438 // JT
439 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000440 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000441 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000442 }
443}
444
445
Chris Lattnered138932005-12-13 06:32:10 +0000446/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
447/// special global used by LLVM. If so, emit it and return true, otherwise
448/// do nothing and return false.
449bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000450 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000451 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000452 EmitLLVMUsedList(GV->getInitializer());
453 return true;
454 }
455
Chris Lattner401e10c2009-07-20 06:14:25 +0000456 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000457 if (GV->getSection() == "llvm.metadata" ||
458 GV->hasAvailableExternallyLinkage())
459 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000460
461 if (!GV->hasAppendingLinkage()) return false;
462
463 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000464
Evan Cheng916d07c2007-06-04 20:39:18 +0000465 const TargetData *TD = TM.getTargetData();
466 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000467 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000468 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000469 EmitAlignment(Align, 0);
470 EmitXXStructorList(GV->getInitializer());
471 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000472 }
473
Chris Lattnerf231c072009-03-09 05:52:15 +0000474 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000475 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000476 EmitAlignment(Align, 0);
477 EmitXXStructorList(GV->getInitializer());
478 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000479 }
480
481 return false;
482}
483
Chris Lattner33adcfb2009-08-22 21:43:10 +0000484/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000485/// global in the specified llvm.used list for which emitUsedDirectiveFor
486/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000487void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000488 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000489
Dan Gohmana119de82009-06-14 23:30:43 +0000490 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000491 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
492 if (InitList == 0) return;
493
494 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000495 const GlobalValue *GV =
496 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000497 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000498 O << Directive;
499 EmitConstantValueOnly(InitList->getOperand(i));
500 O << '\n';
501 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000502 }
503}
504
Chris Lattnered138932005-12-13 06:32:10 +0000505/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
506/// function pointers, ignoring the init priority.
507void AsmPrinter::EmitXXStructorList(Constant *List) {
508 // Should be an array of '{ int, void ()* }' structs. The first value is the
509 // init priority, which we ignore.
510 if (!isa<ConstantArray>(List)) return;
511 ConstantArray *InitList = cast<ConstantArray>(List);
512 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
513 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
514 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000515
516 if (CS->getOperand(1)->isNullValue())
517 return; // Found a null terminator, exit printing.
518 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000519 EmitGlobalConstant(CS->getOperand(1));
520 }
521}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000522
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000523
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000524//===----------------------------------------------------------------------===//
525/// LEB 128 number encoding.
526
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000527/// PrintULEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000528/// representing an unsigned leb128 value.
529void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000530 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000531 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000532 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000533 Value >>= 7;
534 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000535 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000536 if (Value) O << ", ";
537 } while (Value);
538}
539
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000540/// PrintSLEB128 - Print a series of hexadecimal values (separated by commas)
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000541/// representing a signed leb128 value.
542void AsmPrinter::PrintSLEB128(int Value) const {
543 int Sign = Value >> (8 * sizeof(Value) - 1);
544 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000545 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000546
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000547 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000548 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000549 Value >>= 7;
550 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
551 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000552 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000553 if (IsMore) O << ", ";
554 } while (IsMore);
555}
556
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000557//===--------------------------------------------------------------------===//
558// Emission and print routines
559//
560
Bob Wilsonc0ef2442009-11-06 22:38:38 +0000561/// PrintHex - Print a value as a hexadecimal value.
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000562///
563void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000564 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000565 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000566}
567
568/// EOL - Print a newline character to asm stream. If a comment is present
569/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000570void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000571 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000572}
Owen Anderson25995092008-07-01 21:16:27 +0000573
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000574void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000575 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000576 O.PadToColumn(MAI->getCommentColumn());
577 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000578 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000579 << Comment;
580 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000581 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000582}
583
Owen Anderson25995092008-07-01 21:16:27 +0000584void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000585 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000586 O.PadToColumn(MAI->getCommentColumn());
587 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000588 << ' '
589 << Comment;
590 }
591 O << '\n';
592}
593
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000594static const char *DecodeDWARFEncoding(unsigned Encoding) {
595 switch (Encoding) {
596 case dwarf::DW_EH_PE_absptr:
597 return "absptr";
598 case dwarf::DW_EH_PE_omit:
599 return "omit";
600 case dwarf::DW_EH_PE_pcrel:
601 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000602 case dwarf::DW_EH_PE_udata4:
603 return "udata4";
604 case dwarf::DW_EH_PE_udata8:
605 return "udata8";
606 case dwarf::DW_EH_PE_sdata4:
607 return "sdata4";
608 case dwarf::DW_EH_PE_sdata8:
609 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000610 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
611 return "pcrel udata4";
612 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
613 return "pcrel sdata4";
614 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
615 return "pcrel udata8";
616 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
617 return "pcrel sdata8";
618 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
619 return "indirect pcrel udata4";
620 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
621 return "indirect pcrel sdata4";
622 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
623 return "indirect pcrel udata8";
624 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
625 return "indirect pcrel sdata8";
626 }
627
628 return 0;
629}
630
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000631void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
632 if (VerboseAsm && *Comment) {
633 O.PadToColumn(MAI->getCommentColumn());
634 O << MAI->getCommentString()
635 << ' '
636 << Comment;
637
638 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
639 O << " (" << EncStr << ')';
640 }
641 O << '\n';
642}
643
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000644/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
645/// unsigned leb128 value.
646void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000647 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000648 O << "\t.uleb128\t"
649 << Value;
650 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000651 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000652 PrintULEB128(Value);
653 }
654}
655
656/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
657/// signed leb128 value.
658void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000659 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000660 O << "\t.sleb128\t"
661 << Value;
662 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000663 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000664 PrintSLEB128(Value);
665 }
666}
667
668/// EmitInt8 - Emit a byte directive and value.
669///
670void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000671 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000672 PrintHex(Value & 0xFF);
673}
674
675/// EmitInt16 - Emit a short directive and value.
676///
677void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000678 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000679 PrintHex(Value & 0xFFFF);
680}
681
682/// EmitInt32 - Emit a long directive and value.
683///
684void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000685 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000686 PrintHex(Value);
687}
688
689/// EmitInt64 - Emit a long long directive and value.
690///
691void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000692 if (MAI->getData64bitsDirective()) {
693 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000694 PrintHex(Value);
695 } else {
696 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000697 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000698 EmitInt32(unsigned(Value));
699 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000700 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000701 EmitInt32(unsigned(Value >> 32));
702 }
703 }
704}
705
706/// toOctal - Convert the low order bits of X into an octal digit.
707///
708static inline char toOctal(int X) {
709 return (X&7)+'0';
710}
711
712/// printStringChar - Print a char, escaped if necessary.
713///
David Greene71847812009-07-14 20:18:05 +0000714static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000715 if (C == '"') {
716 O << "\\\"";
717 } else if (C == '\\') {
718 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000719 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000720 O << C;
721 } else {
722 switch(C) {
723 case '\b': O << "\\b"; break;
724 case '\f': O << "\\f"; break;
725 case '\n': O << "\\n"; break;
726 case '\r': O << "\\r"; break;
727 case '\t': O << "\\t"; break;
728 default:
729 O << '\\';
730 O << toOctal(C >> 6);
731 O << toOctal(C >> 3);
732 O << toOctal(C >> 0);
733 break;
734 }
735 }
736}
737
738/// EmitString - Emit a string with quotes and a null terminator.
739/// Special characters are emitted properly.
740/// \literal (Eg. '\t') \endliteral
Devang Patele9a05972009-11-24 19:42:17 +0000741void AsmPrinter::EmitString(const StringRef String) const {
Dan Gohman24f8e292009-11-13 21:55:31 +0000742 EmitString(String.data(), String.size());
Bill Wendlingf34be822009-04-09 23:51:31 +0000743}
744
745void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000746 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000747 if (AscizDirective)
748 O << AscizDirective;
749 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000750 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000751 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000752 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000753 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000754 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000755 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000756 else
757 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000758}
759
760
Dan Gohman189f80d2007-09-24 20:58:13 +0000761/// EmitFile - Emit a .file directive.
762void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
763 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000764 for (unsigned i = 0, N = Name.size(); i < N; ++i)
765 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000766 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000767}
768
769
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000770//===----------------------------------------------------------------------===//
771
Chris Lattner3a420532007-05-31 18:57:45 +0000772// EmitAlignment - Emit an alignment directive to the specified power of
773// two boundary. For example, if you pass in 3 here, you will get an 8
774// byte alignment. If a global value is specified, and if that global has
775// an explicit alignment requested, it will unconditionally override the
776// alignment request. However, if ForcedAlignBits is specified, this value
777// has final say: the ultimate alignment will be the max of ForcedAlignBits
778// and the alignment computed with NumBits and the global.
779//
780// The algorithm is:
781// Align = NumBits;
782// if (GV && GV->hasalignment) Align = GV->getalignment();
783// Align = std::max(Align, ForcedAlignBits);
784//
785void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000786 unsigned ForcedAlignBits,
787 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000788 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000789 NumBits = Log2_32(GV->getAlignment());
790 NumBits = std::max(NumBits, ForcedAlignBits);
791
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000792 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000793
794 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000795 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000796 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000797
798 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000799}
David Greenea5bb59f2009-08-05 21:00:52 +0000800
Chris Lattner25045bd2005-11-21 07:51:36 +0000801/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000802///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000803void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000804 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000805 if (MAI->getZeroDirective()) {
806 O << MAI->getZeroDirective() << NumZeros;
807 if (MAI->getZeroDirectiveSuffix())
808 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000809 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000810 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000811 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000812 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000813 }
814 }
815}
816
Chris Lattnera80ba712004-08-16 23:15:22 +0000817// Print out the specified constant, without a storage class. Only the
818// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000819void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000820 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000821 O << '0';
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000822 return;
823 }
824
825 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000826 O << CI->getZExtValue();
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000827 return;
828 }
829
830 if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000831 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000832 // name of the variable or function as the address value.
833 O << Mang->getMangledName(GV);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000834 return;
835 }
836
837 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Dan Gohman6a55e622009-10-30 01:45:18 +0000838 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000839 return;
840 }
841
842 const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV);
843 if (CE == 0) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000844 llvm_unreachable("Unknown constant value!");
Chris Lattnerfe0e7ed2010-01-13 04:29:19 +0000845 O << '0';
846 return;
847 }
848
849 switch (CE->getOpcode()) {
850 case Instruction::ZExt:
851 case Instruction::SExt:
852 case Instruction::FPTrunc:
853 case Instruction::FPExt:
854 case Instruction::UIToFP:
855 case Instruction::SIToFP:
856 case Instruction::FPToUI:
857 case Instruction::FPToSI:
858 default:
859 llvm_unreachable("FIXME: Don't support this constant cast expr");
860 case Instruction::GetElementPtr: {
861 // generate a symbolic expression for the byte address
862 const TargetData *TD = TM.getTargetData();
863 const Constant *ptrVal = CE->getOperand(0);
864 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
865 int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
866 idxVec.size());
867 if (Offset == 0)
868 return EmitConstantValueOnly(ptrVal);
869
870 // Truncate/sext the offset to the pointer size.
871 if (TD->getPointerSizeInBits() != 64) {
872 int SExtAmount = 64-TD->getPointerSizeInBits();
873 Offset = (Offset << SExtAmount) >> SExtAmount;
874 }
875
876 if (Offset)
877 O << '(';
878 EmitConstantValueOnly(ptrVal);
879 if (Offset > 0)
880 O << ") + " << Offset;
881 else
882 O << ") - " << -Offset;
883 return;
884 }
885 case Instruction::BitCast:
886 return EmitConstantValueOnly(CE->getOperand(0));
887
888 case Instruction::IntToPtr: {
889 // Handle casts to pointers by changing them into casts to the appropriate
890 // integer type. This promotes constant folding and simplifies this code.
891 const TargetData *TD = TM.getTargetData();
892 Constant *Op = CE->getOperand(0);
893 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
894 false/*ZExt*/);
895 return EmitConstantValueOnly(Op);
896 }
897
898 case Instruction::PtrToInt: {
899 // Support only foldable casts to/from pointers that can be eliminated by
900 // changing the pointer to the appropriately sized integer type.
901 Constant *Op = CE->getOperand(0);
902 const Type *Ty = CE->getType();
903 const TargetData *TD = TM.getTargetData();
904
905 // We can emit the pointer value into this slot if the slot is an
906 // integer slot greater or equal to the size of the pointer.
907 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
908 return EmitConstantValueOnly(Op);
909
910 O << "((";
911 EmitConstantValueOnly(Op);
912 APInt ptrMask =
913 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
914
915 SmallString<40> S;
916 ptrMask.toStringUnsigned(S);
917 O << ") & " << S.str() << ')';
918 return;
919 }
920
921 case Instruction::Trunc:
922 // We emit the value and depend on the assembler to truncate the generated
923 // expression properly. This is important for differences between
924 // blockaddress labels. Since the two labels are in the same function, it
925 // is reasonable to treat their delta as a 32-bit value.
926 return EmitConstantValueOnly(CE->getOperand(0));
927
928 case Instruction::Add:
929 case Instruction::Sub:
930 case Instruction::And:
931 case Instruction::Or:
932 case Instruction::Xor:
933 O << '(';
934 EmitConstantValueOnly(CE->getOperand(0));
935 O << ')';
936 switch (CE->getOpcode()) {
937 case Instruction::Add:
938 O << " + ";
939 break;
940 case Instruction::Sub:
941 O << " - ";
942 break;
943 case Instruction::And:
944 O << " & ";
945 break;
946 case Instruction::Or:
947 O << " | ";
948 break;
949 case Instruction::Xor:
950 O << " ^ ";
951 break;
952 default:
953 break;
954 }
955 O << '(';
956 EmitConstantValueOnly(CE->getOperand(1));
957 O << ')';
958 break;
Chris Lattnera80ba712004-08-16 23:15:22 +0000959 }
960}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000961
Chris Lattner2980cef2005-11-10 18:06:33 +0000962/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000963/// the predicate isString is true.
964///
David Greene71847812009-07-14 20:18:05 +0000965static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000966 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000967 assert(CVA->isString() && "Array is not string compatible!");
968
Dan Gohmand19a53b2008-06-30 22:03:41 +0000969 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000970 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000971 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000972 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000973 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000974 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000975 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000976}
977
Jeff Cohenc884db42006-05-02 01:16:28 +0000978/// EmitString - Emit a zero-byte-terminated string constant.
979///
980void AsmPrinter::EmitString(const ConstantArray *CVA) const {
981 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000982 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000983 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000984 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000985 printAsCString(O, CVA, NumElts-1);
986 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000987 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000988 printAsCString(O, CVA, NumElts);
989 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000990 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000991}
992
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000993void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
994 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000995 if (CVA->isString()) {
996 EmitString(CVA);
997 } else { // Not a string. Print the values in successive locations
998 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000999 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001000 }
1001}
1002
1003void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
1004 const VectorType *PTy = CP->getType();
1005
1006 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
1007 EmitGlobalConstant(CP->getOperand(I));
1008}
1009
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001010void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1011 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001012 // Print the fields in successive locations. Pad to align if needed!
1013 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +00001014 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001015 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1016 uint64_t sizeSoFar = 0;
1017 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1018 const Constant* field = CVS->getOperand(i);
1019
1020 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +00001021 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +00001022 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1023 - cvsLayout->getElementOffset(i)) - fieldSize;
1024 sizeSoFar += fieldSize + padSize;
1025
1026 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001027 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001028
1029 // Insert padding - this may include padding to increase the size of the
1030 // current field up to the ABI size (if the struct is not packed) as well
1031 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001032 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001033 }
1034 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1035 "Layout of constant struct may be incorrect!");
1036}
1037
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001038void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1039 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001040 // FP Constants are printed as integer constants to avoid losing
1041 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001042 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001043 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001044 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001045 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1046 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001047 if (MAI->getData64bitsDirective(AddrSpace)) {
1048 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001049 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001050 O.PadToColumn(MAI->getCommentColumn());
1051 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001052 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001053 O << '\n';
1054 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001055 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001056 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001057 O.PadToColumn(MAI->getCommentColumn());
1058 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001059 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001060 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001061 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001062 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001063 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001064 O.PadToColumn(MAI->getCommentColumn());
1065 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001066 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001067 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001068 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001069 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001070 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001071 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001072 O.PadToColumn(MAI->getCommentColumn());
1073 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001074 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001075 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001076 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001077 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001078 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001079 O.PadToColumn(MAI->getCommentColumn());
1080 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001081 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001082 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001083 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001084 }
1085 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001086 }
1087
1088 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001089 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001090 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001091 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001092 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001093 O.PadToColumn(MAI->getCommentColumn());
1094 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001095 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001096 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001097 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001098 }
1099
1100 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001101 // all long double variants are printed as hex
1102 // api needed to prevent premature destruction
1103 APInt api = CFP->getValueAPF().bitcastToAPInt();
1104 const uint64_t *p = api.getRawData();
1105 // Convert to double so we can print the approximate val as a comment.
1106 APFloat DoubleVal = CFP->getValueAPF();
1107 bool ignored;
1108 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1109 &ignored);
1110 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001111 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001112 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001113 O.PadToColumn(MAI->getCommentColumn());
1114 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001115 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001116 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001117 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001118 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001119 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001120 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001121 O.PadToColumn(MAI->getCommentColumn());
1122 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001123 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001124 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001125 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001126 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001127 O.PadToColumn(MAI->getCommentColumn());
1128 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001129 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001130 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001131 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001132 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001133 O.PadToColumn(MAI->getCommentColumn());
1134 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001135 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001136 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001137 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001138 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001139 O.PadToColumn(MAI->getCommentColumn());
1140 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001141 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001143 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001144 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001145 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001146 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001147 O.PadToColumn(MAI->getCommentColumn());
1148 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001149 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001150 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001151 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001152 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001153 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001154 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001155 O.PadToColumn(MAI->getCommentColumn());
1156 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001157 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001158 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001159 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001160 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001162 O.PadToColumn(MAI->getCommentColumn());
1163 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001164 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001165 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001166 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001167 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001169 O.PadToColumn(MAI->getCommentColumn());
1170 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001171 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001172 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001173 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001174 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001175 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001176 O.PadToColumn(MAI->getCommentColumn());
1177 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001178 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001179 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001180 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001181 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001182 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1183 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001184 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001185 }
1186
1187 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001188 // all long double variants are printed as hex
1189 // api needed to prevent premature destruction
1190 APInt api = CFP->getValueAPF().bitcastToAPInt();
1191 const uint64_t *p = api.getRawData();
1192 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001193 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001194 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001195 O.PadToColumn(MAI->getCommentColumn());
1196 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001197 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001198 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001199 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001200 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001201 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001202 O.PadToColumn(MAI->getCommentColumn());
1203 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001204 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001205 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001206 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001207 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001208 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001209 O.PadToColumn(MAI->getCommentColumn());
1210 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001211 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001212 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001213 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001214 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001215 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001216 O.PadToColumn(MAI->getCommentColumn());
1217 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001218 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001219 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001220 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001221 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001222 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001223 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001224 O.PadToColumn(MAI->getCommentColumn());
1225 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001226 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001227 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001228 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001229 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001230 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001231 O.PadToColumn(MAI->getCommentColumn());
1232 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001233 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001234 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001235 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001236 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001237 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001238 O.PadToColumn(MAI->getCommentColumn());
1239 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001240 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001241 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001242 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001243 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001244 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001245 O.PadToColumn(MAI->getCommentColumn());
1246 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001247 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001248 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001249 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001250 }
1251 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001252 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001253}
1254
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001255void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1256 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001257 const TargetData *TD = TM.getTargetData();
1258 unsigned BitWidth = CI->getBitWidth();
Chris Lattner38c2b0a2010-01-13 04:39:46 +00001259 assert((BitWidth & 63) == 0 && "only support multiples of 64-bits");
Dan Gohman00d448a2008-12-22 21:14:27 +00001260
1261 // We don't expect assemblers to support integer data directives
1262 // for more than 64 bits, so we emit the data in at most 64-bit
1263 // quantities at a time.
1264 const uint64_t *RawData = CI->getValue().getRawData();
1265 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1266 uint64_t Val;
1267 if (TD->isBigEndian())
1268 Val = RawData[e - i - 1];
1269 else
1270 Val = RawData[i];
1271
Chris Lattner5979dff2010-01-13 04:38:16 +00001272 if (MAI->getData64bitsDirective(AddrSpace)) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001273 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Chris Lattner5979dff2010-01-13 04:38:16 +00001274 continue;
Dan Gohman00d448a2008-12-22 21:14:27 +00001275 }
Chris Lattner5979dff2010-01-13 04:38:16 +00001276
1277 // Emit two 32-bit chunks, order depends on endianness.
1278 unsigned FirstChunk = unsigned(Val), SecondChunk = unsigned(Val >> 32);
1279 const char *FirstName = " least", *SecondName = " most";
1280 if (TD->isBigEndian()) {
1281 std::swap(FirstChunk, SecondChunk);
1282 std::swap(FirstName, SecondName);
1283 }
1284
1285 O << MAI->getData32bitsDirective(AddrSpace) << FirstChunk;
1286 if (VerboseAsm) {
1287 O.PadToColumn(MAI->getCommentColumn());
1288 O << MAI->getCommentString()
1289 << FirstName << " significant half of i64 " << Val;
1290 }
1291 O << '\n';
1292
1293 O << MAI->getData32bitsDirective(AddrSpace) << SecondChunk;
1294 if (VerboseAsm) {
1295 O.PadToColumn(MAI->getCommentColumn());
1296 O << MAI->getCommentString()
1297 << SecondName << " significant half of i64 " << Val;
1298 }
1299 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001300 }
1301}
1302
Chris Lattner25045bd2005-11-21 07:51:36 +00001303/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001304void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001305 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001306 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001307 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001308
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001309 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001310 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001311 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001312 }
1313
1314 if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001315 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001316 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001317 }
1318
1319 if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001320 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001321 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001322 }
1323
1324 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001325 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001326 return;
Chris Lattner3cc3a002010-01-13 04:34:19 +00001327 }
1328
1329 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1330 // If we can directly emit an 8-byte constant, do it.
1331 if (Size == 8)
1332 if (const char *Data64Dir = MAI->getData64bitsDirective(AddrSpace)) {
1333 O << Data64Dir << CI->getZExtValue() << '\n';
1334 return;
1335 }
1336
Dan Gohman00d448a2008-12-22 21:14:27 +00001337 // Small integers are handled below; large integers are handled here.
1338 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001339 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001340 return;
1341 }
Chris Lattner3cc3a002010-01-13 04:34:19 +00001342 }
1343
1344 if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001345 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001346 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001347 }
1348
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001349 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001350 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001351 if (VerboseAsm) {
1352 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1353 SmallString<40> S;
1354 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001355 O.PadToColumn(MAI->getCommentColumn());
1356 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001357 }
Scott Micheleefc8452008-06-03 15:39:51 +00001358 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001359 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001360}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001361
Chris Lattnerfad86b02008-08-17 07:19:36 +00001362void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001363 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001364 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001365}
1366
Chris Lattner3ce9b672006-09-26 23:59:50 +00001367/// PrintSpecial - Print information related to the specified machine instr
1368/// that is independent of the operand, and may be independent of the instr
1369/// itself. This can be useful for portably encoding the comment character
1370/// or other bits of target-specific knowledge into the asmstrings. The
1371/// syntax used is ${:comment}. Targets can override this to add support
1372/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001373void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001374 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001375 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001376 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001377 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001378 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001379 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001380 // Comparing the address of MI isn't sufficient, because machineinstrs may
1381 // be allocated to the same address across functions.
1382 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1383
Owen Andersonbd58edf2009-06-24 22:28:12 +00001384 // If this is a new LastFn instruction, bump the counter.
1385 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001386 ++Counter;
1387 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001388 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001389 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001390 O << Counter;
1391 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001392 std::string msg;
1393 raw_string_ostream Msg(msg);
1394 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001395 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001396 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001397 }
1398}
1399
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001400/// processDebugLoc - Processes the debug information of each machine
1401/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001402void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1403 bool BeforePrintingInsn) {
Devang Patel53bb5c92009-11-10 23:06:00 +00001404 if (!MAI || !DW || !MAI->doesSupportDebugInformation()
1405 || !DW->ShouldEmitDwarfDebug())
Chris Lattnerd2503292009-08-05 04:09:18 +00001406 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001407 DebugLoc DL = MI->getDebugLoc();
Devang Patel53bb5c92009-11-10 23:06:00 +00001408 if (DL.isUnknown())
1409 return;
1410 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1411 if (CurDLT.Scope == 0)
1412 return;
1413
1414 if (BeforePrintingInsn) {
1415 if (CurDLT != PrevDLT) {
1416 unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1417 CurDLT.Scope);
1418 printLabel(L);
Dan Gohmaneecb9912009-12-05 01:42:34 +00001419 O << '\n';
Devang Patel53bb5c92009-11-10 23:06:00 +00001420 DW->BeginScope(MI, L);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001421 PrevDLT = CurDLT;
1422 }
Devang Patel53bb5c92009-11-10 23:06:00 +00001423 } else {
1424 // After printing instruction
1425 DW->EndScope(MI);
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001426 }
1427}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001428
Devang Patel53bb5c92009-11-10 23:06:00 +00001429
Chris Lattner0264d1a2006-01-27 02:10:10 +00001430/// printInlineAsm - This method formats and prints the specified machine
1431/// instruction that is an inline asm.
1432void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001433 unsigned NumOperands = MI->getNumOperands();
1434
1435 // Count the number of register definitions.
1436 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001437 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001438 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001439 assert(NumDefs != NumOperands-1 && "No asm string?");
1440
Dan Gohmand735b802008-10-03 15:45:36 +00001441 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001442
1443 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001444 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001445
Dan Gohman40c57862009-11-06 00:04:54 +00001446 O << '\t';
1447
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001448 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1449 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001450 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001451 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1452 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001453 return;
1454 }
1455
Chris Lattner33adcfb2009-08-22 21:43:10 +00001456 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001457
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001458 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001459 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001460
Chris Lattner66099132006-02-01 22:41:11 +00001461 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1462 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001463
Chris Lattner66099132006-02-01 22:41:11 +00001464 while (*LastEmitted) {
1465 switch (*LastEmitted) {
1466 default: {
1467 // Not a special case, emit the string section literally.
1468 const char *LiteralEnd = LastEmitted+1;
1469 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001470 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001471 ++LiteralEnd;
1472 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1473 O.write(LastEmitted, LiteralEnd-LastEmitted);
1474 LastEmitted = LiteralEnd;
1475 break;
1476 }
Chris Lattner1c059972006-05-05 21:47:05 +00001477 case '\n':
1478 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001479 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001480 break;
Chris Lattner66099132006-02-01 22:41:11 +00001481 case '$': {
1482 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001483 bool Done = true;
1484
1485 // Handle escapes.
1486 switch (*LastEmitted) {
1487 default: Done = false; break;
1488 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001489 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1490 O << '$';
1491 ++LastEmitted; // Consume second '$' character.
1492 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001493 case '(': // $( -> same as GCC's { character.
1494 ++LastEmitted; // Consume '(' character.
1495 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001496 llvm_report_error("Nested variants found in inline asm string: '"
1497 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001498 }
1499 CurVariant = 0; // We're in the first variant now.
1500 break;
1501 case '|':
1502 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001503 if (CurVariant == -1)
1504 O << '|'; // this is gcc's behavior for | outside a variant
1505 else
1506 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001507 break;
1508 case ')': // $) -> same as GCC's } char.
1509 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001510 if (CurVariant == -1)
1511 O << '}'; // this is gcc's behavior for } outside a variant
1512 else
1513 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001514 break;
Chris Lattner66099132006-02-01 22:41:11 +00001515 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001516 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001517
1518 bool HasCurlyBraces = false;
1519 if (*LastEmitted == '{') { // ${variable}
1520 ++LastEmitted; // Consume '{' character.
1521 HasCurlyBraces = true;
1522 }
1523
Chris Lattner3e0cc262009-03-10 05:37:13 +00001524 // If we have ${:foo}, then this is not a real operand reference, it is a
1525 // "magic" string reference, just like in .td files. Arrange to call
1526 // PrintSpecial.
1527 if (HasCurlyBraces && *LastEmitted == ':') {
1528 ++LastEmitted;
1529 const char *StrStart = LastEmitted;
1530 const char *StrEnd = strchr(StrStart, '}');
1531 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001532 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1533 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001534 }
1535
1536 std::string Val(StrStart, StrEnd);
1537 PrintSpecial(MI, Val.c_str());
1538 LastEmitted = StrEnd+1;
1539 break;
1540 }
1541
Chris Lattner66099132006-02-01 22:41:11 +00001542 const char *IDStart = LastEmitted;
1543 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001544 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001545 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1546 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001547 llvm_report_error("Bad $ operand number in inline asm string: '"
1548 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001549 }
1550 LastEmitted = IDEnd;
1551
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001552 char Modifier[2] = { 0, 0 };
1553
Chris Lattner66099132006-02-01 22:41:11 +00001554 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001555 // If we have curly braces, check for a modifier character. This
1556 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1557 if (*LastEmitted == ':') {
1558 ++LastEmitted; // Consume ':' character.
1559 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001560 llvm_report_error("Bad ${:} expression in inline asm string: '"
1561 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001562 }
1563
1564 Modifier[0] = *LastEmitted;
1565 ++LastEmitted; // Consume modifier character.
1566 }
1567
Chris Lattner66099132006-02-01 22:41:11 +00001568 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001569 llvm_report_error("Bad ${} expression in inline asm string: '"
1570 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001571 }
1572 ++LastEmitted; // Consume '}' character.
1573 }
1574
1575 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001576 llvm_report_error("Invalid $ operand number in inline asm string: '"
1577 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001578 }
1579
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001580 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001581 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001582 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1583 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001584
1585 bool Error = false;
1586
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001587 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001588 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001589 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001590 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001591 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001592 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001593
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001594 if (OpNo >= MI->getNumOperands()) {
1595 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001596 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001597 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001598 ++OpNo; // Skip over the ID number.
1599
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001600 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001601 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1602 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001603 else {
1604 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001605 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001606 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1607 Modifier[0] ? Modifier : 0);
1608 } else {
1609 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1610 Modifier[0] ? Modifier : 0);
1611 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001612 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001613 }
1614 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001615 std::string msg;
1616 raw_string_ostream Msg(msg);
1617 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001618 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001619 MI->print(Msg);
1620 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001621 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001622 }
Chris Lattner66099132006-02-01 22:41:11 +00001623 break;
1624 }
Chris Lattner66099132006-02-01 22:41:11 +00001625 }
1626 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001627 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001628}
1629
Evan Chengda47e6e2008-03-15 00:03:38 +00001630/// printImplicitDef - This method prints the specified machine instruction
1631/// that is an implicit def.
1632void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001633 if (!VerboseAsm) return;
1634 O.PadToColumn(MAI->getCommentColumn());
1635 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001636 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001637}
1638
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001639void AsmPrinter::printKill(const MachineInstr *MI) const {
1640 if (!VerboseAsm) return;
1641 O.PadToColumn(MAI->getCommentColumn());
1642 O << MAI->getCommentString() << " kill:";
1643 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1644 const MachineOperand &op = MI->getOperand(n);
1645 assert(op.isReg() && "KILL instruction must have only register operands");
1646 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1647 }
1648}
1649
Jim Laskey1ee29252007-01-26 14:34:52 +00001650/// printLabel - This method prints a local label used by debug and
1651/// exception handling tables.
1652void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001653 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001654}
1655
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001656void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001657 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001658}
1659
Chris Lattner66099132006-02-01 22:41:11 +00001660/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1661/// instruction, using the specified assembler variant. Targets should
Dale Johannesencf0b7662010-01-14 21:50:17 +00001662/// override this to format as appropriate.
Chris Lattner66099132006-02-01 22:41:11 +00001663bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001664 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001665 // Target doesn't support this yet!
1666 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001667}
Chris Lattnerdd260332006-02-24 20:21:58 +00001668
1669bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1670 unsigned AsmVariant,
1671 const char *ExtraCode) {
1672 // Target doesn't support this yet!
1673 return true;
1674}
Nate Begeman37efe672006-04-22 18:53:45 +00001675
Dan Gohman29cbade2009-11-20 23:18:13 +00001676MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA,
1677 const char *Suffix) const {
1678 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock(), Suffix);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001679}
1680
1681MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
Dan Gohman29cbade2009-11-20 23:18:13 +00001682 const BasicBlock *BB,
1683 const char *Suffix) const {
Dan Gohman8c2b5252009-10-30 01:27:03 +00001684 assert(BB->hasName() &&
1685 "Address of anonymous basic block not supported yet!");
1686
Dan Gohman568a3be2009-11-05 23:14:35 +00001687 // This code must use the function name itself, and not the function number,
1688 // since it must be possible to generate the label name from within other
1689 // functions.
Chris Lattner2f8cc262010-01-13 07:30:49 +00001690 SmallString<60> FnName;
1691 Mang->getNameWithPrefix(FnName, F, false);
Dan Gohman8c2b5252009-10-30 01:27:03 +00001692
Chris Lattner2f8cc262010-01-13 07:30:49 +00001693 // FIXME: THIS IS BROKEN IF THE LLVM BASIC BLOCK DOESN'T HAVE A NAME!
Chris Lattner48130352010-01-13 06:38:18 +00001694 SmallString<60> NameResult;
Chris Lattner2f8cc262010-01-13 07:30:49 +00001695 Mang->getNameWithPrefix(NameResult,
1696 StringRef("BA") + Twine((unsigned)FnName.size()) +
1697 "_" + FnName.str() + "_" + BB->getName() + Suffix,
1698 Mangler::Private);
Dan Gohman568a3be2009-11-05 23:14:35 +00001699
Chris Lattner48130352010-01-13 06:38:18 +00001700 return OutContext.GetOrCreateSymbol(NameResult.str());
Dan Gohman8c2b5252009-10-30 01:27:03 +00001701}
1702
Chris Lattner7cb384d2009-09-12 23:02:08 +00001703MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1704 SmallString<60> Name;
1705 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1706 << getFunctionNumber() << '_' << MBBID;
1707
1708 return OutContext.GetOrCreateSymbol(Name.str());
1709}
1710
Chris Lattner6b04ede2010-01-15 23:18:17 +00001711/// GetGlobalValueSymbol - Return the MCSymbol for the specified global
1712/// value.
1713MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
1714 SmallString<60> NameStr;
1715 Mang->getNameWithPrefix(NameStr, GV, false);
1716 return OutContext.GetOrCreateSymbol(NameStr.str());
1717}
1718
Chris Lattnerd588b972010-01-15 23:25:11 +00001719/// GetPrivateGlobalValueSymbolStub - Return the MCSymbol for a symbol with
1720/// global value name as its base, with the specified suffix, and where the
1721/// symbol is forced to have private linkage.
1722MCSymbol *AsmPrinter::GetPrivateGlobalValueSymbolStub(const GlobalValue *GV,
1723 StringRef Suffix) const {
1724 SmallString<60> NameStr;
1725 Mang->getNameWithPrefix(NameStr, GV, true);
1726 NameStr.append(Suffix.begin(), Suffix.end());
1727 return OutContext.GetOrCreateSymbol(NameStr.str());
1728}
1729
Chris Lattner6b04ede2010-01-15 23:18:17 +00001730/// GetExternalSymbolSymbol - Return the MCSymbol for the specified
1731/// ExternalSymbol.
1732MCSymbol *AsmPrinter::GetExternalSymbolSymbol(StringRef Sym) const {
1733 SmallString<60> NameStr;
1734 Mang->getNameWithPrefix(NameStr, Sym);
1735 return OutContext.GetOrCreateSymbol(NameStr.str());
1736}
1737
Chris Lattner7cb384d2009-09-12 23:02:08 +00001738
Chris Lattner70a54c02009-09-13 18:25:37 +00001739/// EmitBasicBlockStart - This method prints the label for the specified
1740/// MachineBasicBlock, an alignment (if present) and a comment describing
1741/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001742void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001743 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001744 if (unsigned Align = MBB->getAlignment())
1745 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001746
Dan Gohmanb1cac332009-10-30 01:34:35 +00001747 // If the block has its address taken, emit a special label to satisfy
1748 // references to the block. This is done so that we don't need to
1749 // remember the number of this label, and so that we can make
1750 // forward references to labels without knowing what their numbers
1751 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001752 if (MBB->hasAddressTaken()) {
1753 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1754 MBB->getBasicBlock())->print(O, MAI);
1755 O << ':';
1756 if (VerboseAsm) {
1757 O.PadToColumn(MAI->getCommentColumn());
1758 O << MAI->getCommentString() << " Address Taken";
1759 }
1760 O << '\n';
1761 }
1762
Dan Gohmanb1cac332009-10-30 01:34:35 +00001763 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001764 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1765 if (VerboseAsm)
1766 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1767 } else {
1768 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1769 O << ':';
1770 if (!VerboseAsm)
1771 O << '\n';
1772 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001773
Dan Gohmanb1cac332009-10-30 01:34:35 +00001774 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001775 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001776 if (const BasicBlock *BB = MBB->getBasicBlock())
1777 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001778 O.PadToColumn(MAI->getCommentColumn());
1779 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001780 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001781 }
David Greeneb71d1b22009-08-10 16:38:07 +00001782
Chris Lattner70a54c02009-09-13 18:25:37 +00001783 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001784 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001785 }
Nate Begeman37efe672006-04-22 18:53:45 +00001786}
Nate Begeman52a51e382006-08-12 21:29:52 +00001787
Evan Chengcc415862007-11-09 01:32:10 +00001788/// printPICJumpTableSetLabel - This method prints a set label for the
1789/// specified MachineBasicBlock for a jumptable entry.
1790void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1791 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001792 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001793 return;
1794
Chris Lattner33adcfb2009-08-22 21:43:10 +00001795 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001796 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001797 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001798 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001799 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001800}
Evan Chengd6594ae2006-09-12 21:00:35 +00001801
Evan Chengcc415862007-11-09 01:32:10 +00001802void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1803 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001804 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001805 return;
1806
Chris Lattner33adcfb2009-08-22 21:43:10 +00001807 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001808 << getFunctionNumber() << '_' << uid << '_' << uid2
1809 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001810 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001811 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001812 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001813}
1814
Evan Chengd6594ae2006-09-12 21:00:35 +00001815/// printDataDirective - This method prints the asm directive for the
1816/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001817void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001818 const TargetData *TD = TM.getTargetData();
1819 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001820 case Type::FloatTyID: case Type::DoubleTyID:
1821 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1822 assert(0 && "Should have already output floating point constant.");
1823 default:
1824 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001825 case Type::IntegerTyID: {
1826 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1827 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001828 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001829 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001830 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001831 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001832 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001833 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001834 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001835 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001836 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001837 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001838 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001839 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001840 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001841 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001842 case Type::PointerTyID:
1843 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001844 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001845 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001846 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001847 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001848 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001849 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001850 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001851 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001852 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001853 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001854 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001855 }
1856}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001857
Chris Lattner53d4d782010-01-15 23:38:51 +00001858void AsmPrinter::printVisibility(const MCSymbol *Sym,
1859 unsigned Visibility) const {
1860 if (Visibility == GlobalValue::HiddenVisibility) {
1861 if (const char *Directive = MAI->getHiddenDirective()) {
1862 O << Directive;
1863 Sym->print(O, MAI);
1864 O << '\n';
1865 }
1866 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1867 if (const char *Directive = MAI->getProtectedDirective()) {
1868 O << Directive;
1869 Sym->print(O, MAI);
1870 O << '\n';
1871 }
1872 }
1873}
1874
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001875void AsmPrinter::printOffset(int64_t Offset) const {
1876 if (Offset > 0)
1877 O << '+' << Offset;
1878 else if (Offset < 0)
1879 O << Offset;
1880}
1881
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001882GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1883 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001884 return 0;
1885
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001886 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001887 if (GCPI != GCMetadataPrinters.end())
1888 return GCPI->second;
1889
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001890 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001891
1892 for (GCMetadataPrinterRegistry::iterator
1893 I = GCMetadataPrinterRegistry::begin(),
1894 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1895 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001896 GCMetadataPrinter *GMP = I->instantiate();
1897 GMP->S = S;
1898 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1899 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001900 }
1901
Chris Lattner103289e2009-08-23 07:19:13 +00001902 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001903 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001904}
David Greene014700c2009-07-13 20:25:48 +00001905
1906/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001907void AsmPrinter::EmitComments(const MachineInstr &MI) const {
David Greene1924aab2009-11-13 21:34:57 +00001908 if (!VerboseAsm)
1909 return;
David Greene3ac1ab82009-07-16 22:24:20 +00001910
David Greene1924aab2009-11-13 21:34:57 +00001911 bool Newline = false;
1912
1913 if (!MI.getDebugLoc().isUnknown()) {
1914 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
1915
1916 // Print source line info.
1917 O.PadToColumn(MAI->getCommentColumn());
Dan Gohman3b9bc042009-12-05 00:23:29 +00001918 O << MAI->getCommentString() << ' ';
1919 DIScope Scope(DLT.Scope);
1920 // Omit the directory, because it's likely to be long and uninteresting.
1921 if (!Scope.isNull())
1922 O << Scope.getFilename();
1923 else
1924 O << "<unknown>";
1925 O << ':' << DLT.Line;
David Greene1924aab2009-11-13 21:34:57 +00001926 if (DLT.Col != 0)
Dan Gohman3b9bc042009-12-05 00:23:29 +00001927 O << ':' << DLT.Col;
David Greene1924aab2009-11-13 21:34:57 +00001928 Newline = true;
David Greene3ac1ab82009-07-16 22:24:20 +00001929 }
David Greene1924aab2009-11-13 21:34:57 +00001930
David Greeneddff9412009-11-16 15:12:23 +00001931 // Check for spills and reloads
1932 int FI;
1933
1934 const MachineFrameInfo *FrameInfo =
1935 MI.getParent()->getParent()->getFrameInfo();
1936
1937 // We assume a single instruction only has a spill or reload, not
1938 // both.
David Greenef7ea2a52009-12-04 22:46:04 +00001939 const MachineMemOperand *MMO;
David Greeneddff9412009-11-16 15:12:23 +00001940 if (TM.getInstrInfo()->isLoadFromStackSlotPostFE(&MI, FI)) {
1941 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001942 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001943 if (Newline) O << '\n';
1944 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001945 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Reload";
David Greeneddff9412009-11-16 15:12:23 +00001946 Newline = true;
1947 }
1948 }
David Greenef7ea2a52009-12-04 22:46:04 +00001949 else if (TM.getInstrInfo()->hasLoadFromStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001950 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1951 if (Newline) O << '\n';
1952 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001953 O << MAI->getCommentString() << ' '
1954 << MMO->getSize() << "-byte Folded Reload";
David Greeneddff9412009-11-16 15:12:23 +00001955 Newline = true;
1956 }
1957 }
1958 else if (TM.getInstrInfo()->isStoreToStackSlotPostFE(&MI, FI)) {
1959 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
David Greenef7ea2a52009-12-04 22:46:04 +00001960 MMO = *MI.memoperands_begin();
David Greeneddff9412009-11-16 15:12:23 +00001961 if (Newline) O << '\n';
1962 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001963 O << MAI->getCommentString() << ' ' << MMO->getSize() << "-byte Spill";
David Greeneddff9412009-11-16 15:12:23 +00001964 Newline = true;
1965 }
1966 }
David Greenef7ea2a52009-12-04 22:46:04 +00001967 else if (TM.getInstrInfo()->hasStoreToStackSlot(&MI, MMO, FI)) {
David Greeneddff9412009-11-16 15:12:23 +00001968 if (FrameInfo->isSpillSlotObjectIndex(FI)) {
1969 if (Newline) O << '\n';
1970 O.PadToColumn(MAI->getCommentColumn());
Dan Gohmanfcafe442009-12-04 23:19:55 +00001971 O << MAI->getCommentString() << ' '
1972 << MMO->getSize() << "-byte Folded Spill";
David Greeneddff9412009-11-16 15:12:23 +00001973 Newline = true;
1974 }
1975 }
1976
1977 // Check for spill-induced copies
1978 unsigned SrcReg, DstReg, SrcSubIdx, DstSubIdx;
1979 if (TM.getInstrInfo()->isMoveInstr(MI, SrcReg, DstReg,
1980 SrcSubIdx, DstSubIdx)) {
1981 if (MI.getAsmPrinterFlag(ReloadReuse)) {
1982 if (Newline) O << '\n';
1983 O.PadToColumn(MAI->getCommentColumn());
1984 O << MAI->getCommentString() << " Reload Reuse";
David Greeneddff9412009-11-16 15:12:23 +00001985 }
1986 }
David Greene014700c2009-07-13 20:25:48 +00001987}
1988
David Greenefe37ab32009-08-18 19:22:55 +00001989/// PrintChildLoopComment - Print comments about child loops within
1990/// the loop for this basic block, with nesting.
1991///
1992static void PrintChildLoopComment(formatted_raw_ostream &O,
1993 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001994 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001995 int FunctionNumber) {
1996 // Add child loop information
1997 for(MachineLoop::iterator cl = loop->begin(),
1998 clend = loop->end();
1999 cl != clend;
2000 ++cl) {
2001 MachineBasicBlock *Header = (*cl)->getHeader();
2002 assert(Header && "No header for loop");
2003
2004 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002005 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00002006
Chris Lattner33adcfb2009-08-22 21:43:10 +00002007 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002008 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002009 << " Child Loop BB" << FunctionNumber << "_"
2010 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
2011
Chris Lattner33adcfb2009-08-22 21:43:10 +00002012 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00002013 }
2014}
2015
David Greeneb71d1b22009-08-10 16:38:07 +00002016/// EmitComments - Pretty-print comments for basic blocks
David Greene1924aab2009-11-13 21:34:57 +00002017void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const {
David Greenefe37ab32009-08-18 19:22:55 +00002018 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00002019 // Add loop depth information
2020 const MachineLoop *loop = LI->getLoopFor(&MBB);
2021
2022 if (loop) {
2023 // Print a newline after bb# annotation.
2024 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00002025 O.PadToColumn(MAI->getCommentColumn());
2026 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00002027 << '\n';
2028
Chris Lattner33adcfb2009-08-22 21:43:10 +00002029 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00002030
2031 MachineBasicBlock *Header = loop->getHeader();
2032 assert(Header && "No header for loop");
2033
2034 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002035 O << MAI->getCommentString() << " Loop Header";
2036 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00002037 }
2038 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00002039 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00002040 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
2041 }
2042
2043 if (loop->empty()) {
2044 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002045 O.PadToColumn(MAI->getCommentColumn());
2046 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00002047 }
2048
2049 // Add parent loop information
2050 for (const MachineLoop *CurLoop = loop->getParentLoop();
2051 CurLoop;
2052 CurLoop = CurLoop->getParentLoop()) {
2053 MachineBasicBlock *Header = CurLoop->getHeader();
2054 assert(Header && "No header for loop");
2055
2056 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00002057 O.PadToColumn(MAI->getCommentColumn());
2058 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00002059 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00002060 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00002061 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
2062 }
2063 }
2064 }
2065}