blob: 61577a5707b649c25bbdec70f1612608fac2b2ca [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/Assembly/Writer.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/Constants.h"
18#include "llvm/Module.h"
Gordon Henriksen1aed5992008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner1b989192007-12-31 04:13:23 +000022#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelfe359e72009-01-13 21:44:10 +000023#include "llvm/CodeGen/DwarfWriter.h"
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +000024#include "llvm/Analysis/DebugInfo.h"
Chris Lattner1eb0ad02009-07-27 21:28:04 +000025#include "llvm/MC/MCContext.h"
David Greene4a37a692009-07-16 22:24:20 +000026#include "llvm/MC/MCInst.h"
Chris Lattnere6ad12f2009-07-31 18:48:30 +000027#include "llvm/MC/MCSection.h"
28#include "llvm/MC/MCStreamer.h"
Evan Cheng42ceb472009-03-25 01:47:28 +000029#include "llvm/Support/CommandLine.h"
Edwin Törökced9ff82009-07-11 13:10:19 +000030#include "llvm/Support/ErrorHandling.h"
David Greene63486122009-07-13 20:25:48 +000031#include "llvm/Support/FormattedStream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032#include "llvm/Support/Mangler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033#include "llvm/Target/TargetAsmInfo.h"
34#include "llvm/Target/TargetData.h"
35#include "llvm/Target/TargetLowering.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000036#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng0eeed442008-07-01 23:18:29 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng3c0eda52008-03-15 00:03:38 +000038#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng6fb06762007-11-09 01:32:10 +000039#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner89b36582008-08-17 07:19:36 +000040#include "llvm/ADT/SmallString.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000041#include "llvm/ADT/StringExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042#include <cerrno>
43using namespace llvm;
44
Evan Cheng42ceb472009-03-25 01:47:28 +000045static cl::opt<cl::boolOrDefault>
46AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
47 cl::init(cl::BOU_UNSET));
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049char AsmPrinter::ID = 0;
David Greene302008d2009-07-14 20:18:05 +000050AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Daniel Dunbarb10d2222009-07-01 01:48:54 +000051 const TargetAsmInfo *T, bool VDef)
52 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Evan Cheng3c0eda52008-03-15 00:03:38 +000053 TM(tm), TAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner1eb0ad02009-07-27 21:28:04 +000054
55 OutContext(*new MCContext()),
56 OutStreamer(*createAsmStreamer(OutContext, O)),
57
Chris Lattnerebd055c2009-08-03 23:20:21 +000058 LastMI(0), LastFn(0), Counter(~0U),
Owen Andersonc82bd822009-06-25 16:55:32 +000059 PrevDLT(0, ~0U, ~0U) {
Chris Lattnerebd055c2009-08-03 23:20:21 +000060 CurrentSection = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +000061 DW = 0; MMI = 0;
Evan Cheng42ceb472009-03-25 01:47:28 +000062 switch (AsmVerbose) {
63 case cl::BOU_UNSET: VerboseAsm = VDef; break;
64 case cl::BOU_TRUE: VerboseAsm = true; break;
65 case cl::BOU_FALSE: VerboseAsm = false; break;
66 }
67}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000069AsmPrinter::~AsmPrinter() {
70 for (gcp_iterator I = GCMetadataPrinters.begin(),
71 E = GCMetadataPrinters.end(); I != E; ++I)
72 delete I->second;
Chris Lattner1eb0ad02009-07-27 21:28:04 +000073
74 delete &OutStreamer;
75 delete &OutContext;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +000076}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Chris Lattner75bdd292009-08-03 19:12:26 +000078TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerc4c40a92009-07-28 03:13:23 +000079 return TM.getTargetLowering()->getObjFileLowering();
80}
81
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000082/// SwitchToSection - Switch to the specified section of the executable if we
Chris Lattner6faaff62009-08-03 18:04:42 +000083/// are not already in it! If "NS" is null, then this causes us to exit the
84/// current section and not reenter another one. This is generally used for
85/// asmprinter hacks.
86///
87/// FIXME: Remove support for null sections.
88///
Chris Lattnere6ad12f2009-07-31 18:48:30 +000089void AsmPrinter::SwitchToSection(const MCSection *NS) {
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000090 // If we're already in this section, we're done.
Chris Lattnerebd055c2009-08-03 23:20:21 +000091 if (CurrentSection == NS) return;
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000092
Chris Lattnerebd055c2009-08-03 23:20:21 +000093 CurrentSection = NS;
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000094
Chris Lattner1c4428a2009-08-05 20:49:52 +000095 if (NS == 0) return;
96
Chris Lattnerd0d09fc2009-08-08 22:41:53 +000097 NS->PrintSwitchToSection(*TAI, O);
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +000098}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000100void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +0000101 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000102 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000103 AU.addRequired<GCModuleInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000104}
105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000107 // Initialize TargetLoweringObjectFile.
108 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
109 .Initialize(OutContext, TM);
110
Bill Wendling4c5482b2009-07-20 21:30:28 +0000111 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattnerc889b3b2009-07-21 17:30:51 +0000112 TAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113
Chris Lattner690b02c2009-06-18 23:41:35 +0000114 if (TAI->doesAllowQuotesInName())
115 Mang->setUseQuotes(true);
116
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000117 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000118 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000119
120 if (TAI->hasSingleParameterDotFile()) {
121 /* Very minimal debug info. It is ignored if we emit actual
122 debug info. If we don't, this at helps the user find where
123 a function came from. */
124 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
125 }
126
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000127 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
128 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
129 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (!M.getModuleInlineAsm().empty())
132 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
133 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000134 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 << " End of file scope inline assembly\n";
136
Chris Lattner0f98c332009-08-03 23:10:34 +0000137 SwitchToSection(0); // Reset back to no section to close off sections.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
Chris Lattner2424eac2009-06-24 19:09:55 +0000139 if (TAI->doesSupportDebugInformation() ||
140 TAI->doesSupportExceptionHandling()) {
141 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
142 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000143 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000144 DW = getAnalysisIfAvailable<DwarfWriter>();
145 if (DW)
146 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel1a454842009-06-19 21:54:26 +0000147 }
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 return false;
150}
151
152bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000153 // Emit global variables.
154 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
155 I != E; ++I)
156 PrintGlobalVariable(I);
157
Chris Lattnere1225cc2009-06-24 18:54:37 +0000158 // Emit final debug information.
159 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
160 DW->EndModule();
161
Chris Lattnerdb191f02009-06-24 18:52:01 +0000162 // If the target wants to know about weak references, print them all.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 if (TAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000164 // FIXME: This is not lazy, it would be nice to only print weak references
165 // to stuff that is actually used. Note that doing so would require targets
166 // to notice uses in operands (due to constant exprs etc). This should
167 // happen with the MC stuff eventually.
Chris Lattner0f98c332009-08-03 23:10:34 +0000168 SwitchToSection(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169
Chris Lattnerdb191f02009-06-24 18:52:01 +0000170 // Print out module-level global variables here.
171 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
172 I != E; ++I) {
173 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000174 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000175 }
176
Chris Lattner0f98c332009-08-03 23:10:34 +0000177 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000178 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000179 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000180 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 }
182
183 if (TAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000184 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000186 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000187 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000188
189 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000190 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000191
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000192 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000193 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000196 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000197 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000198
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000199 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000200
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000201 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 }
203 }
204
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000205 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000206 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
207 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
208 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
209 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000210
Dan Gohmana65530a2008-05-05 00:28:39 +0000211 // If we don't have any trampolines, then we don't require stack memory
212 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000213 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000214 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
215 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000216 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000219 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000220
221 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 return false;
223}
224
Chris Lattner5c3475e2009-07-17 20:46:40 +0000225std::string
226AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000227 assert(MF && "No machine function?");
Chris Lattner5c3475e2009-07-17 20:46:40 +0000228 return Mang->getMangledName(MF->getFunction(), ".eh",
229 TAI->is_EHSymbolPrivate());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000230}
231
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
233 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000234 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000235 IncrementFunctionNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236}
237
Evan Cheng68c18682009-03-13 07:51:59 +0000238namespace {
239 // SectionCPs - Keep track the alignment, constpool entries per Section.
240 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000241 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000242 unsigned Alignment;
243 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000244 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000245 };
246}
247
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248/// EmitConstantPool - Print to the current output stream assembly
249/// representations of the constants in the constant pool MCP. This is
250/// used to print out constants which have been "spilled to memory" by
251/// the code generator.
252///
253void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
254 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
255 if (CP.empty()) return;
256
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000257 // Calculate sections for constant pool entries. We collect entries to go into
258 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000259 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000261 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000262 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000263
264 SectionKind Kind;
265 switch (CPE.getRelocationInfo()) {
266 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000267 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000268 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000269 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000270 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000271 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000272 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000273 case 4: Kind = SectionKind::getMergeableConst4(); break;
274 case 8: Kind = SectionKind::getMergeableConst8(); break;
275 case 16: Kind = SectionKind::getMergeableConst16();break;
276 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000277 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000278 }
279
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000280 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000281
Evan Cheng68c18682009-03-13 07:51:59 +0000282 // The number of sections are small, just do a linear search from the
283 // last section to the first.
284 bool Found = false;
285 unsigned SecIdx = CPSections.size();
286 while (SecIdx != 0) {
287 if (CPSections[--SecIdx].S == S) {
288 Found = true;
289 break;
290 }
291 }
292 if (!Found) {
293 SecIdx = CPSections.size();
294 CPSections.push_back(SectionCPs(S, Align));
295 }
296
297 if (Align > CPSections[SecIdx].Alignment)
298 CPSections[SecIdx].Alignment = Align;
299 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000300 }
301
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000302 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000303 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
304 SwitchToSection(CPSections[i].S);
305 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306
Evan Cheng68c18682009-03-13 07:51:59 +0000307 unsigned Offset = 0;
308 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
309 unsigned CPI = CPSections[i].CPEs[j];
310 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000313 unsigned AlignMask = CPE.getAlignment() - 1;
314 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
315 EmitZeros(NewOffset - Offset);
316
317 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000318 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000319
320 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
321 << CPI << ":\t\t\t\t\t";
322 if (VerboseAsm) {
323 O << TAI->getCommentString() << ' ';
324 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000325 }
Evan Cheng68c18682009-03-13 07:51:59 +0000326 O << '\n';
327 if (CPE.isMachineConstantPoolEntry())
328 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
329 else
330 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 }
332 }
333}
334
335/// EmitJumpTableInfo - Print assembly representations of the jump tables used
336/// by the current function to the current output stream.
337///
338void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
339 MachineFunction &MF) {
340 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
341 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
344
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 // Pick the directive to use to print the jump table entries, and switch to
346 // the appropriate section.
347 TargetLowering *LoweringInfo = TM.getTargetLowering();
348
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000349 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000350 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000351 if (F->isWeakForLinker() ||
352 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 // In PIC mode, we need to emit the jump table to the same section as the
354 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000355 // We should also do if the section name is NULL or function is declared in
356 // discardable section.
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000357 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000358 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000359 // Otherwise, drop it in the readonly section.
360 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000361 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000362 SwitchToSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000363 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
365
366 EmitAlignment(Log2_32(MJTI->getAlignment()));
367
368 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
369 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
370
371 // If this jump table was deleted, ignore it.
372 if (JTBBs.empty()) continue;
373
374 // For PIC codegen, if possible we want to use the SetDirective to reduce
375 // the number of relocations the assembler will generate for the jump table.
376 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000377 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 if (TAI->getSetDirective() && IsPic)
379 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000380 if (EmittedSets.insert(JTBBs[ii]))
381 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382
383 // On some targets (e.g. darwin) we want to emit two consequtive labels
384 // before each jump table. The first label is never referenced, but tells
385 // the assembler and linker the extents of the jump table object. The
386 // second label is actually referenced by the code.
Evan Chengccca6f72009-06-18 20:37:15 +0000387 if (JTInDiffSection) {
388 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
389 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
390 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391
Evan Cheng477013c2007-10-14 05:57:21 +0000392 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
393 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394
395 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000396 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 O << '\n';
398 }
399 }
400}
401
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000402void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
403 const MachineBasicBlock *MBB,
404 unsigned uid) const {
405 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
406
407 // Use JumpTableDirective otherwise honor the entry size from the jump table
408 // info.
409 const char *JTEntryDirective = TAI->getJumpTableDirective();
410 bool HadJTEntryDirective = JTEntryDirective != NULL;
411 if (!HadJTEntryDirective) {
412 JTEntryDirective = MJTI->getEntrySize() == 4 ?
413 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
414 }
415
416 O << JTEntryDirective << ' ';
417
418 // If we have emitted set directives for the jump table entries, print
419 // them rather than the entries themselves. If we're emitting PIC, then
420 // emit the table entries as differences between two text section labels.
421 // If we're emitting non-PIC code, then emit the entries as direct
422 // references to the target basic blocks.
423 if (IsPic) {
424 if (TAI->getSetDirective()) {
425 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
426 << '_' << uid << "_set_" << MBB->getNumber();
427 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000428 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000429 // If the arch uses custom Jump Table directives, don't calc relative to
430 // JT
431 if (!HadJTEntryDirective)
432 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
433 << getFunctionNumber() << '_' << uid;
434 }
435 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000436 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000437 }
438}
439
440
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000441/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
442/// special global used by LLVM. If so, emit it and return true, otherwise
443/// do nothing and return false.
444bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000445 if (GV->getName() == "llvm.used") {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000446 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
447 EmitLLVMUsedList(GV->getInitializer());
448 return true;
449 }
450
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000451 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000452 if (GV->getSection() == "llvm.metadata" ||
453 GV->hasAvailableExternallyLinkage())
454 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455
456 if (!GV->hasAppendingLinkage()) return false;
457
458 assert(GV->hasInitializer() && "Not a special LLVM global!");
459
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 const TargetData *TD = TM.getTargetData();
461 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000462 if (GV->getName() == "llvm.global_ctors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000463 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000464 EmitAlignment(Align, 0);
465 EmitXXStructorList(GV->getInitializer());
466 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467 }
468
Chris Lattner0c433e92009-03-09 05:52:15 +0000469 if (GV->getName() == "llvm.global_dtors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000470 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000471 EmitAlignment(Align, 0);
472 EmitXXStructorList(GV->getInitializer());
473 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 }
475
476 return false;
477}
478
479/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000480/// global in the specified llvm.used list for which emitUsedDirectiveFor
481/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482void AsmPrinter::EmitLLVMUsedList(Constant *List) {
483 const char *Directive = TAI->getUsedDirective();
484
Dan Gohman9e1657f2009-06-14 23:30:43 +0000485 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000486 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
487 if (InitList == 0) return;
488
489 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000490 const GlobalValue *GV =
491 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000492 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000493 O << Directive;
494 EmitConstantValueOnly(InitList->getOperand(i));
495 O << '\n';
496 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 }
498}
499
500/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
501/// function pointers, ignoring the init priority.
502void AsmPrinter::EmitXXStructorList(Constant *List) {
503 // Should be an array of '{ int, void ()* }' structs. The first value is the
504 // init priority, which we ignore.
505 if (!isa<ConstantArray>(List)) return;
506 ConstantArray *InitList = cast<ConstantArray>(List);
507 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
508 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
509 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
510
511 if (CS->getOperand(1)->isNullValue())
512 return; // Found a null terminator, exit printing.
513 // Emit the function pointer.
514 EmitGlobalConstant(CS->getOperand(1));
515 }
516}
517
518/// getGlobalLinkName - Returns the asm/link name of of the specified
519/// global variable. Should be overridden by each target asm printer to
520/// generate the appropriate value.
Bill Wendling26a8ab92009-04-10 00:12:49 +0000521const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
522 std::string &LinkName) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523 if (isa<Function>(GV)) {
524 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000525 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 LinkName += TAI->getFunctionAddrSuffix();
527 } else {
528 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000529 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000530 LinkName += TAI->getGlobalVarAddrSuffix();
531 }
532
533 return LinkName;
534}
535
536/// EmitExternalGlobal - Emit the external reference to a global variable.
537/// Should be overridden if an indirect reference should be used.
538void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000539 std::string GLN;
540 O << getGlobalLinkName(GV, GLN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541}
542
543
544
545//===----------------------------------------------------------------------===//
546/// LEB 128 number encoding.
547
548/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
549/// representing an unsigned leb128 value.
550void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000551 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000553 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 Value >>= 7;
555 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000556 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000557 if (Value) O << ", ";
558 } while (Value);
559}
560
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000561/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
562/// representing a signed leb128 value.
563void AsmPrinter::PrintSLEB128(int Value) const {
564 int Sign = Value >> (8 * sizeof(Value) - 1);
565 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000566 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000567
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000569 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000570 Value >>= 7;
571 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
572 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000573 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000574 if (IsMore) O << ", ";
575 } while (IsMore);
576}
577
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000578//===--------------------------------------------------------------------===//
579// Emission and print routines
580//
581
582/// PrintHex - Print a value as a hexidecimal value.
583///
584void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000585 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000586 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000587}
588
589/// EOL - Print a newline character to asm stream. If a comment is present
590/// then it will be printed first. Comments should not contain '\n'.
591void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000592 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000593}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000594
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000595void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000596 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000597 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000599 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000600 << Comment;
601 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000602 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603}
604
Owen Anderson367bfbb2008-07-01 21:16:27 +0000605void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000606 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000607 O << '\t'
608 << TAI->getCommentString()
609 << ' '
610 << Comment;
611 }
612 O << '\n';
613}
614
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000615/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
616/// unsigned leb128 value.
617void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
618 if (TAI->hasLEB128()) {
619 O << "\t.uleb128\t"
620 << Value;
621 } else {
622 O << TAI->getData8bitsDirective();
623 PrintULEB128(Value);
624 }
625}
626
627/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
628/// signed leb128 value.
629void AsmPrinter::EmitSLEB128Bytes(int Value) const {
630 if (TAI->hasLEB128()) {
631 O << "\t.sleb128\t"
632 << Value;
633 } else {
634 O << TAI->getData8bitsDirective();
635 PrintSLEB128(Value);
636 }
637}
638
639/// EmitInt8 - Emit a byte directive and value.
640///
641void AsmPrinter::EmitInt8(int Value) const {
642 O << TAI->getData8bitsDirective();
643 PrintHex(Value & 0xFF);
644}
645
646/// EmitInt16 - Emit a short directive and value.
647///
648void AsmPrinter::EmitInt16(int Value) const {
649 O << TAI->getData16bitsDirective();
650 PrintHex(Value & 0xFFFF);
651}
652
653/// EmitInt32 - Emit a long directive and value.
654///
655void AsmPrinter::EmitInt32(int Value) const {
656 O << TAI->getData32bitsDirective();
657 PrintHex(Value);
658}
659
660/// EmitInt64 - Emit a long long directive and value.
661///
662void AsmPrinter::EmitInt64(uint64_t Value) const {
663 if (TAI->getData64bitsDirective()) {
664 O << TAI->getData64bitsDirective();
665 PrintHex(Value);
666 } else {
667 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000668 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000669 EmitInt32(unsigned(Value));
670 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000671 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000672 EmitInt32(unsigned(Value >> 32));
673 }
674 }
675}
676
677/// toOctal - Convert the low order bits of X into an octal digit.
678///
679static inline char toOctal(int X) {
680 return (X&7)+'0';
681}
682
683/// printStringChar - Print a char, escaped if necessary.
684///
David Greene302008d2009-07-14 20:18:05 +0000685static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000686 if (C == '"') {
687 O << "\\\"";
688 } else if (C == '\\') {
689 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000690 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000691 O << C;
692 } else {
693 switch(C) {
694 case '\b': O << "\\b"; break;
695 case '\f': O << "\\f"; break;
696 case '\n': O << "\\n"; break;
697 case '\r': O << "\\r"; break;
698 case '\t': O << "\\t"; break;
699 default:
700 O << '\\';
701 O << toOctal(C >> 6);
702 O << toOctal(C >> 3);
703 O << toOctal(C >> 0);
704 break;
705 }
706 }
707}
708
709/// EmitString - Emit a string with quotes and a null terminator.
710/// Special characters are emitted properly.
711/// \literal (Eg. '\t') \endliteral
712void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000713 EmitString(String.c_str(), String.size());
714}
715
716void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000717 const char* AscizDirective = TAI->getAscizDirective();
718 if (AscizDirective)
719 O << AscizDirective;
720 else
721 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000722 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000723 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000724 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000725 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000726 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000727 else
728 O << "\\0\"";
729}
730
731
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000732/// EmitFile - Emit a .file directive.
733void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
734 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000735 for (unsigned i = 0, N = Name.size(); i < N; ++i)
736 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000737 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000738}
739
740
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000741//===----------------------------------------------------------------------===//
742
743// EmitAlignment - Emit an alignment directive to the specified power of
744// two boundary. For example, if you pass in 3 here, you will get an 8
745// byte alignment. If a global value is specified, and if that global has
746// an explicit alignment requested, it will unconditionally override the
747// alignment request. However, if ForcedAlignBits is specified, this value
748// has final say: the ultimate alignment will be the max of ForcedAlignBits
749// and the alignment computed with NumBits and the global.
750//
751// The algorithm is:
752// Align = NumBits;
753// if (GV && GV->hasalignment) Align = GV->getalignment();
754// Align = std::max(Align, ForcedAlignBits);
755//
756void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000757 unsigned ForcedAlignBits,
758 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000759 if (GV && GV->getAlignment())
760 NumBits = Log2_32(GV->getAlignment());
761 NumBits = std::max(NumBits, ForcedAlignBits);
762
763 if (NumBits == 0) return; // No need to emit alignment.
764 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000765 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000766
Chris Lattnerebd055c2009-08-03 23:20:21 +0000767 if (CurrentSection && CurrentSection->getKind().isText())
768 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
769 O << ',';
770 PrintHex(FillValue);
771 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000772 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000773}
David Greenecdc2de32009-08-05 21:00:52 +0000774
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000775/// EmitZeros - Emit a block of zeros.
776///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000777void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000778 if (NumZeros) {
779 if (TAI->getZeroDirective()) {
780 O << TAI->getZeroDirective() << NumZeros;
781 if (TAI->getZeroDirectiveSuffix())
782 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000783 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784 } else {
785 for (; NumZeros; --NumZeros)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000786 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000787 }
788 }
789}
790
791// Print out the specified constant, without a storage class. Only the
792// constants valid in constant expressions can occur here.
793void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
794 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000795 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000797 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000798 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
799 // This is a constant address for a global variable or function. Use the
800 // name of the variable or function as the address value, possibly
801 // decorating it with GlobalVarAddrPrefix/Suffix or
802 // FunctionAddrPrefix/Suffix (these all default to "" )
803 if (isa<Function>(GV)) {
804 O << TAI->getFunctionAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000805 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000806 << TAI->getFunctionAddrSuffix();
807 } else {
808 O << TAI->getGlobalVarAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000809 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 << TAI->getGlobalVarAddrSuffix();
811 }
812 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
813 const TargetData *TD = TM.getTargetData();
814 unsigned Opcode = CE->getOpcode();
815 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000816 case Instruction::Trunc:
817 case Instruction::ZExt:
818 case Instruction::SExt:
819 case Instruction::FPTrunc:
820 case Instruction::FPExt:
821 case Instruction::UIToFP:
822 case Instruction::SIToFP:
823 case Instruction::FPToUI:
824 case Instruction::FPToSI:
825 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000826 case Instruction::GetElementPtr: {
827 // generate a symbolic expression for the byte address
828 const Constant *ptrVal = CE->getOperand(0);
829 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
830 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
831 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000832 // Truncate/sext the offset to the pointer size.
833 if (TD->getPointerSizeInBits() != 64) {
834 int SExtAmount = 64-TD->getPointerSizeInBits();
835 Offset = (Offset << SExtAmount) >> SExtAmount;
836 }
837
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000839 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000840 EmitConstantValueOnly(ptrVal);
841 if (Offset > 0)
842 O << ") + " << Offset;
843 else if (Offset < 0)
844 O << ") - " << -Offset;
845 } else {
846 EmitConstantValueOnly(ptrVal);
847 }
848 break;
849 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000850 case Instruction::BitCast:
851 return EmitConstantValueOnly(CE->getOperand(0));
852
853 case Instruction::IntToPtr: {
854 // Handle casts to pointers by changing them into casts to the appropriate
855 // integer type. This promotes constant folding and simplifies this code.
856 Constant *Op = CE->getOperand(0);
857 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
858 return EmitConstantValueOnly(Op);
859 }
860
861
862 case Instruction::PtrToInt: {
863 // Support only foldable casts to/from pointers that can be eliminated by
864 // changing the pointer to the appropriately sized integer type.
865 Constant *Op = CE->getOperand(0);
866 const Type *Ty = CE->getType();
867
868 // We can emit the pointer value into this slot if the slot is an
869 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000870 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000871 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000872
873 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000874 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000875 APInt ptrMask =
876 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000877
878 SmallString<40> S;
879 ptrMask.toStringUnsigned(S);
880 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000881 break;
882 }
883 case Instruction::Add:
884 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000885 case Instruction::And:
886 case Instruction::Or:
887 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000888 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000889 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000890 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000891 switch (Opcode) {
892 case Instruction::Add:
893 O << " + ";
894 break;
895 case Instruction::Sub:
896 O << " - ";
897 break;
898 case Instruction::And:
899 O << " & ";
900 break;
901 case Instruction::Or:
902 O << " | ";
903 break;
904 case Instruction::Xor:
905 O << " ^ ";
906 break;
907 default:
908 break;
909 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000910 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000911 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000912 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 break;
914 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000915 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000916 }
917 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000918 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000919 }
920}
921
922/// printAsCString - Print the specified array as a C compatible string, only if
923/// the predicate isString is true.
924///
David Greene302008d2009-07-14 20:18:05 +0000925static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000926 unsigned LastElt) {
927 assert(CVA->isString() && "Array is not string compatible!");
928
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000929 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000930 for (unsigned i = 0; i != LastElt; ++i) {
931 unsigned char C =
932 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
933 printStringChar(O, C);
934 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000935 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000936}
937
938/// EmitString - Emit a zero-byte-terminated string constant.
939///
940void AsmPrinter::EmitString(const ConstantArray *CVA) const {
941 unsigned NumElts = CVA->getNumOperands();
942 if (TAI->getAscizDirective() && NumElts &&
943 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
944 O << TAI->getAscizDirective();
945 printAsCString(O, CVA, NumElts-1);
946 } else {
947 O << TAI->getAsciiDirective();
948 printAsCString(O, CVA, NumElts);
949 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000950 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951}
952
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000953void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
954 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000955 if (CVA->isString()) {
956 EmitString(CVA);
957 } else { // Not a string. Print the values in successive locations
958 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000959 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000960 }
961}
962
963void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
964 const VectorType *PTy = CP->getType();
965
966 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
967 EmitGlobalConstant(CP->getOperand(I));
968}
969
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000970void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
971 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000972 // Print the fields in successive locations. Pad to align if needed!
973 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000974 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000975 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
976 uint64_t sizeSoFar = 0;
977 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
978 const Constant* field = CVS->getOperand(i);
979
980 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000981 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +0000982 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
983 - cvsLayout->getElementOffset(i)) - fieldSize;
984 sizeSoFar += fieldSize + padSize;
985
986 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000987 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000988
989 // Insert padding - this may include padding to increase the size of the
990 // current field up to the ABI size (if the struct is not packed) as well
991 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000992 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000993 }
994 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
995 "Layout of constant struct may be incorrect!");
996}
997
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000998void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
999 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001000 // FP Constants are printed as integer constants to avoid losing
1001 // precision...
1002 const TargetData *TD = TM.getTargetData();
1003 if (CFP->getType() == Type::DoubleTy) {
1004 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1005 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Cheng11db8142009-03-24 00:17:40 +00001006 if (TAI->getData64bitsDirective(AddrSpace)) {
1007 O << TAI->getData64bitsDirective(AddrSpace) << i;
1008 if (VerboseAsm)
1009 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1010 O << '\n';
1011 } else if (TD->isBigEndian()) {
1012 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1013 if (VerboseAsm)
1014 O << '\t' << TAI->getCommentString()
1015 << " double most significant word " << Val;
1016 O << '\n';
1017 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1018 if (VerboseAsm)
1019 O << '\t' << TAI->getCommentString()
1020 << " double least significant word " << Val;
1021 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001022 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001023 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1024 if (VerboseAsm)
1025 O << '\t' << TAI->getCommentString()
1026 << " double least significant word " << Val;
1027 O << '\n';
1028 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1029 if (VerboseAsm)
1030 O << '\t' << TAI->getCommentString()
1031 << " double most significant word " << Val;
1032 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001033 }
1034 return;
1035 } else if (CFP->getType() == Type::FloatTy) {
1036 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001037 O << TAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001038 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1039 if (VerboseAsm)
1040 O << '\t' << TAI->getCommentString() << " float " << Val;
1041 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001042 return;
1043 } else if (CFP->getType() == Type::X86_FP80Ty) {
1044 // all long double variants are printed as hex
1045 // api needed to prevent premature destruction
1046 APInt api = CFP->getValueAPF().bitcastToAPInt();
1047 const uint64_t *p = api.getRawData();
1048 // Convert to double so we can print the approximate val as a comment.
1049 APFloat DoubleVal = CFP->getValueAPF();
1050 bool ignored;
1051 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1052 &ignored);
1053 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001054 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1055 if (VerboseAsm)
1056 O << '\t' << TAI->getCommentString()
1057 << " long double most significant halfword of ~"
1058 << DoubleVal.convertToDouble();
1059 O << '\n';
1060 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1061 if (VerboseAsm)
1062 O << '\t' << TAI->getCommentString() << " long double next halfword";
1063 O << '\n';
1064 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1065 if (VerboseAsm)
1066 O << '\t' << TAI->getCommentString() << " long double next halfword";
1067 O << '\n';
1068 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1069 if (VerboseAsm)
1070 O << '\t' << TAI->getCommentString() << " long double next halfword";
1071 O << '\n';
1072 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1073 if (VerboseAsm)
1074 O << '\t' << TAI->getCommentString()
1075 << " long double least significant halfword";
1076 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001077 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001078 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1079 if (VerboseAsm)
1080 O << '\t' << TAI->getCommentString()
1081 << " long double least significant halfword of ~"
1082 << DoubleVal.convertToDouble();
1083 O << '\n';
1084 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1085 if (VerboseAsm)
1086 O << '\t' << TAI->getCommentString()
1087 << " long double next halfword";
1088 O << '\n';
1089 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1090 if (VerboseAsm)
1091 O << '\t' << TAI->getCommentString()
1092 << " long double next halfword";
1093 O << '\n';
1094 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1095 if (VerboseAsm)
1096 O << '\t' << TAI->getCommentString()
1097 << " long double next halfword";
1098 O << '\n';
1099 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1100 if (VerboseAsm)
1101 O << '\t' << TAI->getCommentString()
1102 << " long double most significant halfword";
1103 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001104 }
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001105 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001106 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001107 return;
1108 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1109 // all long double variants are printed as hex
1110 // api needed to prevent premature destruction
1111 APInt api = CFP->getValueAPF().bitcastToAPInt();
1112 const uint64_t *p = api.getRawData();
1113 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001114 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1115 if (VerboseAsm)
1116 O << '\t' << TAI->getCommentString()
1117 << " long double most significant word";
1118 O << '\n';
1119 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1120 if (VerboseAsm)
1121 O << '\t' << TAI->getCommentString()
1122 << " long double next word";
1123 O << '\n';
1124 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1125 if (VerboseAsm)
1126 O << '\t' << TAI->getCommentString()
1127 << " long double next word";
1128 O << '\n';
1129 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1130 if (VerboseAsm)
1131 O << '\t' << TAI->getCommentString()
1132 << " long double least significant word";
1133 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001134 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001135 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1136 if (VerboseAsm)
1137 O << '\t' << TAI->getCommentString()
1138 << " long double least significant word";
1139 O << '\n';
1140 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1141 if (VerboseAsm)
1142 O << '\t' << TAI->getCommentString()
1143 << " long double next word";
1144 O << '\n';
1145 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1146 if (VerboseAsm)
1147 O << '\t' << TAI->getCommentString()
1148 << " long double next word";
1149 O << '\n';
1150 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1151 if (VerboseAsm)
1152 O << '\t' << TAI->getCommentString()
1153 << " long double most significant word";
1154 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001155 }
1156 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001157 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001158}
1159
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001160void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1161 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001162 const TargetData *TD = TM.getTargetData();
1163 unsigned BitWidth = CI->getBitWidth();
1164 assert(isPowerOf2_32(BitWidth) &&
1165 "Non-power-of-2-sized integers not handled!");
1166
1167 // We don't expect assemblers to support integer data directives
1168 // for more than 64 bits, so we emit the data in at most 64-bit
1169 // quantities at a time.
1170 const uint64_t *RawData = CI->getValue().getRawData();
1171 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1172 uint64_t Val;
1173 if (TD->isBigEndian())
1174 Val = RawData[e - i - 1];
1175 else
1176 Val = RawData[i];
1177
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001178 if (TAI->getData64bitsDirective(AddrSpace))
1179 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001180 else if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001181 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1182 if (VerboseAsm)
1183 O << '\t' << TAI->getCommentString()
1184 << " Double-word most significant word " << Val;
1185 O << '\n';
1186 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1187 if (VerboseAsm)
1188 O << '\t' << TAI->getCommentString()
1189 << " Double-word least significant word " << Val;
1190 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001191 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001192 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1193 if (VerboseAsm)
1194 O << '\t' << TAI->getCommentString()
1195 << " Double-word least significant word " << Val;
1196 O << '\n';
1197 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1198 if (VerboseAsm)
1199 O << '\t' << TAI->getCommentString()
1200 << " Double-word most significant word " << Val;
1201 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001202 }
1203 }
1204}
1205
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001206/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001207void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001208 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001209 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001210 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001211
1212 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001213 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001214 return;
1215 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001216 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001217 return;
1218 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001219 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001220 return;
1221 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001222 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001223 return;
1224 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1225 // Small integers are handled below; large integers are handled here.
1226 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001227 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001228 return;
1229 }
1230 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001231 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001232 return;
1233 }
1234
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001235 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001236 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001237 if (VerboseAsm) {
1238 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1239 SmallString<40> S;
1240 CI->getValue().toStringUnsigned(S, 16);
1241 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1242 }
Scott Michele067c3c2008-06-03 15:39:51 +00001243 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001244 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001245}
1246
Chris Lattner89b36582008-08-17 07:19:36 +00001247void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001248 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001249 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001250}
1251
1252/// PrintSpecial - Print information related to the specified machine instr
1253/// that is independent of the operand, and may be independent of the instr
1254/// itself. This can be useful for portably encoding the comment character
1255/// or other bits of target-specific knowledge into the asmstrings. The
1256/// syntax used is ${:comment}. Targets can override this to add support
1257/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001258void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001259 if (!strcmp(Code, "private")) {
1260 O << TAI->getPrivateGlobalPrefix();
1261 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001262 if (VerboseAsm)
1263 O << TAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001265 // Comparing the address of MI isn't sufficient, because machineinstrs may
1266 // be allocated to the same address across functions.
1267 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1268
Owen Andersonda3388b2009-06-24 22:28:12 +00001269 // If this is a new LastFn instruction, bump the counter.
1270 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001271 ++Counter;
1272 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001273 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 }
1275 O << Counter;
1276 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001277 std::string msg;
1278 raw_string_ostream Msg(msg);
1279 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001281 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 }
1283}
1284
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001285/// processDebugLoc - Processes the debug information of each machine
1286/// instruction's DebugLoc.
1287void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner693cfcc2009-08-05 04:09:18 +00001288 if (!TAI || !DW)
1289 return;
1290
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001291 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1292 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001293 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1294
1295 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1296 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1297 DICompileUnit(CurDLT.CompileUnit)));
1298
1299 PrevDLT = CurDLT;
1300 }
1301 }
1302}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303
1304/// printInlineAsm - This method formats and prints the specified machine
1305/// instruction that is an inline asm.
1306void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1307 unsigned NumOperands = MI->getNumOperands();
1308
1309 // Count the number of register definitions.
1310 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001311 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 ++NumDefs)
1313 assert(NumDefs != NumOperands-1 && "No asm string?");
1314
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001315 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001316
1317 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1318 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1319
Dale Johannesene99fc902008-01-29 02:21:21 +00001320 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1321 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001322 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001323 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001324 return;
1325 }
1326
1327 O << TAI->getInlineAsmStart() << "\n\t";
1328
1329 // The variant of the current asmprinter.
1330 int AsmPrinterVariant = TAI->getAssemblerDialect();
1331
1332 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1333 const char *LastEmitted = AsmStr; // One past the last character emitted.
1334
1335 while (*LastEmitted) {
1336 switch (*LastEmitted) {
1337 default: {
1338 // Not a special case, emit the string section literally.
1339 const char *LiteralEnd = LastEmitted+1;
1340 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1341 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1342 ++LiteralEnd;
1343 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1344 O.write(LastEmitted, LiteralEnd-LastEmitted);
1345 LastEmitted = LiteralEnd;
1346 break;
1347 }
1348 case '\n':
1349 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001350 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001351 break;
1352 case '$': {
1353 ++LastEmitted; // Consume '$' character.
1354 bool Done = true;
1355
1356 // Handle escapes.
1357 switch (*LastEmitted) {
1358 default: Done = false; break;
1359 case '$': // $$ -> $
1360 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1361 O << '$';
1362 ++LastEmitted; // Consume second '$' character.
1363 break;
1364 case '(': // $( -> same as GCC's { character.
1365 ++LastEmitted; // Consume '(' character.
1366 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001367 llvm_report_error("Nested variants found in inline asm string: '"
1368 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001369 }
1370 CurVariant = 0; // We're in the first variant now.
1371 break;
1372 case '|':
1373 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001374 if (CurVariant == -1)
1375 O << '|'; // this is gcc's behavior for | outside a variant
1376 else
1377 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001378 break;
1379 case ')': // $) -> same as GCC's } char.
1380 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001381 if (CurVariant == -1)
1382 O << '}'; // this is gcc's behavior for } outside a variant
1383 else
1384 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001385 break;
1386 }
1387 if (Done) break;
1388
1389 bool HasCurlyBraces = false;
1390 if (*LastEmitted == '{') { // ${variable}
1391 ++LastEmitted; // Consume '{' character.
1392 HasCurlyBraces = true;
1393 }
1394
Chris Lattner6af48032009-03-10 05:37:13 +00001395 // If we have ${:foo}, then this is not a real operand reference, it is a
1396 // "magic" string reference, just like in .td files. Arrange to call
1397 // PrintSpecial.
1398 if (HasCurlyBraces && *LastEmitted == ':') {
1399 ++LastEmitted;
1400 const char *StrStart = LastEmitted;
1401 const char *StrEnd = strchr(StrStart, '}');
1402 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001403 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1404 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001405 }
1406
1407 std::string Val(StrStart, StrEnd);
1408 PrintSpecial(MI, Val.c_str());
1409 LastEmitted = StrEnd+1;
1410 break;
1411 }
1412
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001413 const char *IDStart = LastEmitted;
1414 char *IDEnd;
1415 errno = 0;
1416 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1417 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001418 llvm_report_error("Bad $ operand number in inline asm string: '"
1419 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001420 }
1421 LastEmitted = IDEnd;
1422
1423 char Modifier[2] = { 0, 0 };
1424
1425 if (HasCurlyBraces) {
1426 // If we have curly braces, check for a modifier character. This
1427 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1428 if (*LastEmitted == ':') {
1429 ++LastEmitted; // Consume ':' character.
1430 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001431 llvm_report_error("Bad ${:} expression in inline asm string: '"
1432 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001433 }
1434
1435 Modifier[0] = *LastEmitted;
1436 ++LastEmitted; // Consume modifier character.
1437 }
1438
1439 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001440 llvm_report_error("Bad ${} expression in inline asm string: '"
1441 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001442 }
1443 ++LastEmitted; // Consume '}' character.
1444 }
1445
1446 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001447 llvm_report_error("Invalid $ operand number in inline asm string: '"
1448 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001449 }
1450
1451 // Okay, we finally have a value number. Ask the target to print this
1452 // operand!
1453 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1454 unsigned OpNo = 1;
1455
1456 bool Error = false;
1457
1458 // Scan to find the machine operand number for the operand.
1459 for (; Val; --Val) {
1460 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001461 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001462 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 }
1464
1465 if (OpNo >= MI->getNumOperands()) {
1466 Error = true;
1467 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001468 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001469 ++OpNo; // Skip over the ID number.
1470
Dale Johannesencfb19e62007-11-05 21:20:28 +00001471 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001472 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001473 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001474 else {
1475 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001476 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001477 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1478 Modifier[0] ? Modifier : 0);
1479 } else {
1480 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1481 Modifier[0] ? Modifier : 0);
1482 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001483 }
1484 }
1485 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001486 std::string msg;
1487 raw_string_ostream Msg(msg);
1488 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001489 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001490 MI->print(Msg);
1491 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001492 }
1493 }
1494 break;
1495 }
1496 }
1497 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001498 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001499}
1500
Evan Cheng3c0eda52008-03-15 00:03:38 +00001501/// printImplicitDef - This method prints the specified machine instruction
1502/// that is an implicit def.
1503void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Cheng11db8142009-03-24 00:17:40 +00001504 if (VerboseAsm)
1505 O << '\t' << TAI->getCommentString() << " implicit-def: "
1506 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001507}
1508
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001509/// printLabel - This method prints a local label used by debug and
1510/// exception handling tables.
1511void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001512 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001513}
1514
Evan Chenga53c40a2008-02-01 09:10:45 +00001515void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001516 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001517}
1518
Evan Cheng2e28d622008-02-02 04:07:54 +00001519/// printDeclare - This method prints a local variable declaration used by
1520/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001521/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1522/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001523void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Patelfe359e72009-01-13 21:44:10 +00001524 unsigned FI = MI->getOperand(0).getIndex();
Evan Chengc439a852008-02-04 23:06:48 +00001525 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001526 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001527}
1528
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001529/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1530/// instruction, using the specified assembler variant. Targets should
1531/// overried this to format as appropriate.
1532bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1533 unsigned AsmVariant, const char *ExtraCode) {
1534 // Target doesn't support this yet!
1535 return true;
1536}
1537
1538bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1539 unsigned AsmVariant,
1540 const char *ExtraCode) {
1541 // Target doesn't support this yet!
1542 return true;
1543}
1544
1545/// printBasicBlockLabel - This method prints the label for the specified
1546/// MachineBasicBlock
1547void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001548 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001549 bool printColon,
1550 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001551 if (printAlign) {
1552 unsigned Align = MBB->getAlignment();
1553 if (Align)
1554 EmitAlignment(Log2_32(Align));
1555 }
1556
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001557 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001558 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001559 if (printColon)
1560 O << ':';
1561 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001562 O << '\t' << TAI->getCommentString() << ' '
Daniel Dunbar5d3ea962009-07-26 09:48:23 +00001563 << MBB->getBasicBlock()->getNameStr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001564}
1565
Evan Cheng6fb06762007-11-09 01:32:10 +00001566/// printPICJumpTableSetLabel - This method prints a set label for the
1567/// specified MachineBasicBlock for a jumptable entry.
1568void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1569 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001570 if (!TAI->getSetDirective())
1571 return;
1572
1573 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001574 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001575 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001576 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1577 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001578}
1579
Evan Cheng6fb06762007-11-09 01:32:10 +00001580void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1581 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 if (!TAI->getSetDirective())
1583 return;
1584
1585 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001586 << getFunctionNumber() << '_' << uid << '_' << uid2
1587 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001588 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001589 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1590 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591}
1592
1593/// printDataDirective - This method prints the asm directive for the
1594/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001595void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596 const TargetData *TD = TM.getTargetData();
1597 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001598 case Type::FloatTyID: case Type::DoubleTyID:
1599 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1600 assert(0 && "Should have already output floating point constant.");
1601 default:
1602 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001603 case Type::IntegerTyID: {
1604 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1605 if (BitWidth <= 8)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001606 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001607 else if (BitWidth <= 16)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001608 O << TAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001609 else if (BitWidth <= 32)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001610 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001611 else if (BitWidth <= 64) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001612 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001613 "Target cannot handle 64-bit constant exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001614 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001615 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001616 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001617 }
1618 break;
1619 }
1620 case Type::PointerTyID:
1621 if (TD->getPointerSize() == 8) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001622 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001624 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001625 } else if (TD->getPointerSize() == 2) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001626 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001627 } else if (TD->getPointerSize() == 1) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001628 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001629 } else {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001630 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001631 }
1632 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001633 }
1634}
1635
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001636void AsmPrinter::printVisibility(const std::string& Name,
1637 unsigned Visibility) const {
1638 if (Visibility == GlobalValue::HiddenVisibility) {
1639 if (const char *Directive = TAI->getHiddenDirective())
1640 O << Directive << Name << '\n';
1641 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1642 if (const char *Directive = TAI->getProtectedDirective())
1643 O << Directive << Name << '\n';
1644 }
1645}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001646
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001647void AsmPrinter::printOffset(int64_t Offset) const {
1648 if (Offset > 0)
1649 O << '+' << Offset;
1650 else if (Offset < 0)
1651 O << Offset;
1652}
1653
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001654GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1655 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001656 return 0;
1657
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001658 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001659 if (GCPI != GCMetadataPrinters.end())
1660 return GCPI->second;
1661
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001662 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001663
1664 for (GCMetadataPrinterRegistry::iterator
1665 I = GCMetadataPrinterRegistry::begin(),
1666 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1667 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001668 GCMetadataPrinter *GMP = I->instantiate();
1669 GMP->S = S;
1670 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1671 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001672 }
1673
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001674 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001675 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001676}
David Greene63486122009-07-13 20:25:48 +00001677
1678/// EmitComments - Pretty-print comments for instructions
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001679void AsmPrinter::EmitComments(const MachineInstr &MI) const {
1680 if (!VerboseAsm ||
1681 MI.getDebugLoc().isUnknown())
1682 return;
1683
1684 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001685
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001686 // Print source line info
1687 O.PadToColumn(TAI->getCommentColumn(), 1);
1688 O << TAI->getCommentString() << " SrcLine ";
1689 if (DLT.CompileUnit->hasInitializer()) {
1690 Constant *Name = DLT.CompileUnit->getInitializer();
1691 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1692 if (NameString->isString())
1693 O << NameString->getAsString() << " ";
David Greene4a37a692009-07-16 22:24:20 +00001694 }
Chris Lattnerfdbeb812009-08-07 23:42:01 +00001695 O << DLT.Line;
1696 if (DLT.Col != 0)
1697 O << ":" << DLT.Col;
David Greene63486122009-07-13 20:25:48 +00001698}
1699
1700/// EmitComments - Pretty-print comments for instructions
1701void AsmPrinter::EmitComments(const MCInst &MI) const
1702{
David Greene1fa35942009-07-22 20:33:26 +00001703 if (VerboseAsm) {
1704 if (!MI.getDebugLoc().isUnknown()) {
1705 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001706
David Greene1fa35942009-07-22 20:33:26 +00001707 // Print source line info
1708 O.PadToColumn(TAI->getCommentColumn(), 1);
1709 O << TAI->getCommentString() << " SrcLine ";
1710 if (DLT.CompileUnit->hasInitializer()) {
1711 Constant *Name = DLT.CompileUnit->getInitializer();
1712 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1713 if (NameString->isString()) {
1714 O << NameString->getAsString() << " ";
1715 }
1716 }
1717 O << DLT.Line;
1718 if (DLT.Col != 0)
1719 O << ":" << DLT.Col;
1720 }
David Greene4a37a692009-07-16 22:24:20 +00001721 }
David Greene63486122009-07-13 20:25:48 +00001722}