blob: 3102bdae017c1df0a64e7504b085e9156d5e7477 [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
97 // If section is named we need to switch into it via special '.section'
98 // directive and also append funky flags. Otherwise - section name is just
99 // some magic assembler directive.
100 if (!NS->isDirective()) {
101 SmallString<32> FlagsStr;
102 getObjFileLowering().getSectionFlagsAsString(NS->getKind(), FlagsStr);
Chris Lattnerd8310522009-07-27 05:32:16 +0000103
Chris Lattner1c4428a2009-08-05 20:49:52 +0000104 O << TAI->getSwitchToSectionDirective()
105 << CurrentSection->getName() << FlagsStr.c_str() << '\n';
106 } else {
107 O << CurrentSection->getName() << '\n';
Anton Korobeynikov1a9edae2008-09-24 22:14:23 +0000108 }
Anton Korobeynikov801e0fd2008-09-24 22:12:10 +0000109}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000111void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohmanecb436f2009-07-31 23:37:33 +0000112 AU.setPreservesAll();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000113 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000114 AU.addRequired<GCModuleInfo>();
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000115}
116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117bool AsmPrinter::doInitialization(Module &M) {
Chris Lattner01316272009-07-31 17:42:42 +0000118 // Initialize TargetLoweringObjectFile.
119 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
120 .Initialize(OutContext, TM);
121
Bill Wendling4c5482b2009-07-20 21:30:28 +0000122 Mang = new Mangler(M, TAI->getGlobalPrefix(), TAI->getPrivateGlobalPrefix(),
Chris Lattnerc889b3b2009-07-21 17:30:51 +0000123 TAI->getLinkerPrivateGlobalPrefix());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
Chris Lattner690b02c2009-06-18 23:41:35 +0000125 if (TAI->doesAllowQuotesInName())
126 Mang->setUseQuotes(true);
127
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000128 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000129 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Rafael Espindola5cf2e552008-12-03 11:01:37 +0000130
131 if (TAI->hasSingleParameterDotFile()) {
132 /* Very minimal debug info. It is ignored if we emit actual
133 debug info. If we don't, this at helps the user find where
134 a function came from. */
135 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
136 }
137
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000138 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
139 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
140 MP->beginAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 if (!M.getModuleInlineAsm().empty())
143 O << TAI->getCommentString() << " Start of file scope inline assembly\n"
144 << M.getModuleInlineAsm()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000145 << '\n' << TAI->getCommentString()
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 << " End of file scope inline assembly\n";
147
Chris Lattner0f98c332009-08-03 23:10:34 +0000148 SwitchToSection(0); // Reset back to no section to close off sections.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149
Chris Lattner2424eac2009-06-24 19:09:55 +0000150 if (TAI->doesSupportDebugInformation() ||
151 TAI->doesSupportExceptionHandling()) {
152 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
153 if (MMI)
Devang Patel1a454842009-06-19 21:54:26 +0000154 MMI->AnalyzeModule(M);
Chris Lattner2424eac2009-06-24 19:09:55 +0000155 DW = getAnalysisIfAvailable<DwarfWriter>();
156 if (DW)
157 DW->BeginModule(&M, MMI, O, this, TAI);
Devang Patel1a454842009-06-19 21:54:26 +0000158 }
159
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 return false;
161}
162
163bool AsmPrinter::doFinalization(Module &M) {
Chris Lattnerae982212009-07-21 18:38:57 +0000164 // Emit global variables.
165 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
166 I != E; ++I)
167 PrintGlobalVariable(I);
168
Chris Lattnere1225cc2009-06-24 18:54:37 +0000169 // Emit final debug information.
170 if (TAI->doesSupportDebugInformation() || TAI->doesSupportExceptionHandling())
171 DW->EndModule();
172
Chris Lattnerdb191f02009-06-24 18:52:01 +0000173 // If the target wants to know about weak references, print them all.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 if (TAI->getWeakRefDirective()) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000175 // FIXME: This is not lazy, it would be nice to only print weak references
176 // to stuff that is actually used. Note that doing so would require targets
177 // to notice uses in operands (due to constant exprs etc). This should
178 // happen with the MC stuff eventually.
Chris Lattner0f98c332009-08-03 23:10:34 +0000179 SwitchToSection(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180
Chris Lattnerdb191f02009-06-24 18:52:01 +0000181 // Print out module-level global variables here.
182 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
183 I != E; ++I) {
184 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000185 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000186 }
187
Chris Lattner0f98c332009-08-03 23:10:34 +0000188 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerdb191f02009-06-24 18:52:01 +0000189 if (I->hasExternalWeakLinkage())
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000190 O << TAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattnerdb191f02009-06-24 18:52:01 +0000191 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 }
193
194 if (TAI->getSetDirective()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000195 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattnerdb191f02009-06-24 18:52:01 +0000197 I != E; ++I) {
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000198 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000199
200 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000201 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000202
Anton Korobeynikov6d2c5062007-09-06 17:21:48 +0000203 if (I->hasExternalLinkage() || !TAI->getWeakRefDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000204 O << "\t.globl\t" << Name << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 else if (I->hasWeakLinkage())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000206 O << TAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolaa168fc92009-01-15 20:18:42 +0000207 else if (!I->hasLocalLinkage())
Edwin Törökbd448e32009-07-14 16:55:14 +0000208 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000209
Anton Korobeynikovb191f5c2008-09-24 22:21:04 +0000210 printVisibility(Name, I->getVisibility());
Anton Korobeynikova6f01832008-03-11 21:41:14 +0000211
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000212 O << TAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 }
214 }
215
Duncan Sands4e0d6a72009-01-28 13:14:17 +0000216 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000217 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
218 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
219 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
220 MP->finishAssembly(O, *this, *TAI);
Gordon Henriksendf87fdc2008-01-07 01:30:38 +0000221
Dan Gohmana65530a2008-05-05 00:28:39 +0000222 // If we don't have any trampolines, then we don't require stack memory
223 // to be executable. Some targets have a directive to declare this.
Chris Lattnerdb191f02009-06-24 18:52:01 +0000224 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana65530a2008-05-05 00:28:39 +0000225 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
226 if (TAI->getNonexecutableStackDirective())
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000227 O << TAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana65530a2008-05-05 00:28:39 +0000228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 delete Mang; Mang = 0;
Chris Lattner2424eac2009-06-24 19:09:55 +0000230 DW = 0; MMI = 0;
Chris Lattner1eb0ad02009-07-27 21:28:04 +0000231
232 OutStreamer.Finish();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 return false;
234}
235
Chris Lattner5c3475e2009-07-17 20:46:40 +0000236std::string
237AsmPrinter::getCurrentFunctionEHName(const MachineFunction *MF) const {
Bill Wendlingef9211a2007-09-18 01:47:22 +0000238 assert(MF && "No machine function?");
Chris Lattner5c3475e2009-07-17 20:46:40 +0000239 return Mang->getMangledName(MF->getFunction(), ".eh",
240 TAI->is_EHSymbolPrivate());
Bill Wendlingef9211a2007-09-18 01:47:22 +0000241}
242
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
244 // What's my mangled name?
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000245 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng477013c2007-10-14 05:57:21 +0000246 IncrementFunctionNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247}
248
Evan Cheng68c18682009-03-13 07:51:59 +0000249namespace {
250 // SectionCPs - Keep track the alignment, constpool entries per Section.
251 struct SectionCPs {
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000252 const MCSection *S;
Evan Cheng68c18682009-03-13 07:51:59 +0000253 unsigned Alignment;
254 SmallVector<unsigned, 4> CPEs;
Chris Lattnere6ad12f2009-07-31 18:48:30 +0000255 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng68c18682009-03-13 07:51:59 +0000256 };
257}
258
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259/// EmitConstantPool - Print to the current output stream assembly
260/// representations of the constants in the constant pool MCP. This is
261/// used to print out constants which have been "spilled to memory" by
262/// the code generator.
263///
264void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
265 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
266 if (CP.empty()) return;
267
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000268 // Calculate sections for constant pool entries. We collect entries to go into
269 // the same section together to reduce amount of section switch statements.
Evan Cheng68c18682009-03-13 07:51:59 +0000270 SmallVector<SectionCPs, 4> CPSections;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner680c6f62009-07-22 00:28:43 +0000272 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng68c18682009-03-13 07:51:59 +0000273 unsigned Align = CPE.getAlignment();
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000274
275 SectionKind Kind;
276 switch (CPE.getRelocationInfo()) {
277 default: llvm_unreachable("Unknown section kind");
Chris Lattnera9453412009-08-01 23:57:16 +0000278 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000279 case 1:
Chris Lattnera9453412009-08-01 23:57:16 +0000280 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattnered0c6762009-07-26 07:00:12 +0000281 break;
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000282 case 0:
Chris Lattnered0c6762009-07-26 07:00:12 +0000283 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattnera9453412009-08-01 23:57:16 +0000284 case 4: Kind = SectionKind::getMergeableConst4(); break;
285 case 8: Kind = SectionKind::getMergeableConst8(); break;
286 case 16: Kind = SectionKind::getMergeableConst16();break;
287 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattnered0c6762009-07-26 07:00:12 +0000288 }
Chris Lattnerfc60ba12009-07-26 06:26:55 +0000289 }
290
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000291 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner680c6f62009-07-22 00:28:43 +0000292
Evan Cheng68c18682009-03-13 07:51:59 +0000293 // The number of sections are small, just do a linear search from the
294 // last section to the first.
295 bool Found = false;
296 unsigned SecIdx = CPSections.size();
297 while (SecIdx != 0) {
298 if (CPSections[--SecIdx].S == S) {
299 Found = true;
300 break;
301 }
302 }
303 if (!Found) {
304 SecIdx = CPSections.size();
305 CPSections.push_back(SectionCPs(S, Align));
306 }
307
308 if (Align > CPSections[SecIdx].Alignment)
309 CPSections[SecIdx].Alignment = Align;
310 CPSections[SecIdx].CPEs.push_back(i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311 }
312
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000313 // Now print stuff into the calculated sections.
Evan Cheng68c18682009-03-13 07:51:59 +0000314 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
315 SwitchToSection(CPSections[i].S);
316 EmitAlignment(Log2_32(CPSections[i].Alignment));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000317
Evan Cheng68c18682009-03-13 07:51:59 +0000318 unsigned Offset = 0;
319 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
320 unsigned CPI = CPSections[i].CPEs[j];
321 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000322
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 // Emit inter-object padding for alignment.
Evan Cheng68c18682009-03-13 07:51:59 +0000324 unsigned AlignMask = CPE.getAlignment() - 1;
325 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
326 EmitZeros(NewOffset - Offset);
327
328 const Type *Ty = CPE.getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +0000329 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng68c18682009-03-13 07:51:59 +0000330
331 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
332 << CPI << ":\t\t\t\t\t";
333 if (VerboseAsm) {
334 O << TAI->getCommentString() << ' ';
335 WriteTypeSymbolic(O, CPE.getType(), 0);
Anton Korobeynikovb866b252008-09-24 22:17:59 +0000336 }
Evan Cheng68c18682009-03-13 07:51:59 +0000337 O << '\n';
338 if (CPE.isMachineConstantPoolEntry())
339 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
340 else
341 EmitGlobalConstant(CPE.Val.ConstVal);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000342 }
343 }
344}
345
346/// EmitJumpTableInfo - Print assembly representations of the jump tables used
347/// by the current function to the current output stream.
348///
349void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
350 MachineFunction &MF) {
351 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
352 if (JT.empty()) return;
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000353
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
355
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 // Pick the directive to use to print the jump table entries, and switch to
357 // the appropriate section.
358 TargetLowering *LoweringInfo = TM.getTargetLowering();
359
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000360 const Function *F = MF.getFunction();
Evan Chengccca6f72009-06-18 20:37:15 +0000361 bool JTInDiffSection = false;
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000362 if (F->isWeakForLinker() ||
363 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 // In PIC mode, we need to emit the jump table to the same section as the
365 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikov7f3fa2c2008-07-09 13:27:16 +0000366 // We should also do if the section name is NULL or function is declared in
367 // discardable section.
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000368 SwitchToSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 } else {
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000370 // Otherwise, drop it in the readonly section.
371 const MCSection *ReadOnlySection =
Chris Lattnera9453412009-08-01 23:57:16 +0000372 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattnerd7f8c0a2009-08-01 23:46:12 +0000373 SwitchToSection(ReadOnlySection);
Evan Chengccca6f72009-06-18 20:37:15 +0000374 JTInDiffSection = true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 }
376
377 EmitAlignment(Log2_32(MJTI->getAlignment()));
378
379 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
380 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
381
382 // If this jump table was deleted, ignore it.
383 if (JTBBs.empty()) continue;
384
385 // For PIC codegen, if possible we want to use the SetDirective to reduce
386 // the number of relocations the assembler will generate for the jump table.
387 // Set directives are all printed before the jump table itself.
Evan Cheng6fb06762007-11-09 01:32:10 +0000388 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000389 if (TAI->getSetDirective() && IsPic)
390 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Cheng6fb06762007-11-09 01:32:10 +0000391 if (EmittedSets.insert(JTBBs[ii]))
392 printPICJumpTableSetLabel(i, JTBBs[ii]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000393
394 // On some targets (e.g. darwin) we want to emit two consequtive labels
395 // before each jump table. The first label is never referenced, but tells
396 // the assembler and linker the extents of the jump table object. The
397 // second label is actually referenced by the code.
Evan Chengccca6f72009-06-18 20:37:15 +0000398 if (JTInDiffSection) {
399 if (const char *JTLabelPrefix = TAI->getJumpTableSpecialLabelPrefix())
400 O << JTLabelPrefix << "JTI" << getFunctionNumber() << '_' << i << ":\n";
401 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402
Evan Cheng477013c2007-10-14 05:57:21 +0000403 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
404 << '_' << i << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405
406 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000407 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000408 O << '\n';
409 }
410 }
411}
412
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000413void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
414 const MachineBasicBlock *MBB,
415 unsigned uid) const {
416 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
417
418 // Use JumpTableDirective otherwise honor the entry size from the jump table
419 // info.
420 const char *JTEntryDirective = TAI->getJumpTableDirective();
421 bool HadJTEntryDirective = JTEntryDirective != NULL;
422 if (!HadJTEntryDirective) {
423 JTEntryDirective = MJTI->getEntrySize() == 4 ?
424 TAI->getData32bitsDirective() : TAI->getData64bitsDirective();
425 }
426
427 O << JTEntryDirective << ' ';
428
429 // If we have emitted set directives for the jump table entries, print
430 // them rather than the entries themselves. If we're emitting PIC, then
431 // emit the table entries as differences between two text section labels.
432 // If we're emitting non-PIC code, then emit the entries as direct
433 // references to the target basic blocks.
434 if (IsPic) {
435 if (TAI->getSetDirective()) {
436 O << TAI->getPrivateGlobalPrefix() << getFunctionNumber()
437 << '_' << uid << "_set_" << MBB->getNumber();
438 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000439 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000440 // If the arch uses custom Jump Table directives, don't calc relative to
441 // JT
442 if (!HadJTEntryDirective)
443 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI"
444 << getFunctionNumber() << '_' << uid;
445 }
446 } else {
Evan Cheng45c1edb2008-02-28 00:43:03 +0000447 printBasicBlockLabel(MBB, false, false, false);
Anton Korobeynikov5772c672007-11-14 09:18:41 +0000448 }
449}
450
451
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000452/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
453/// special global used by LLVM. If so, emit it and return true, otherwise
454/// do nothing and return false.
455bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbare03513b2009-07-25 23:55:21 +0000456 if (GV->getName() == "llvm.used") {
Andrew Lenharth61d35f52007-08-22 19:33:11 +0000457 if (TAI->getUsedDirective() != 0) // No need to emit this at all.
458 EmitLLVMUsedList(GV->getInitializer());
459 return true;
460 }
461
Chris Lattner1e0e0d12009-07-20 06:14:25 +0000462 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner68433442009-04-13 05:44:34 +0000463 if (GV->getSection() == "llvm.metadata" ||
464 GV->hasAvailableExternallyLinkage())
465 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466
467 if (!GV->hasAppendingLinkage()) return false;
468
469 assert(GV->hasInitializer() && "Not a special LLVM global!");
470
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000471 const TargetData *TD = TM.getTargetData();
472 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattner0c433e92009-03-09 05:52:15 +0000473 if (GV->getName() == "llvm.global_ctors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000474 SwitchToSection(getObjFileLowering().getStaticCtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000475 EmitAlignment(Align, 0);
476 EmitXXStructorList(GV->getInitializer());
477 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 }
479
Chris Lattner0c433e92009-03-09 05:52:15 +0000480 if (GV->getName() == "llvm.global_dtors") {
Chris Lattnerb5c952a2009-08-02 00:34:36 +0000481 SwitchToSection(getObjFileLowering().getStaticDtorSection());
Chris Lattner1e0e4892009-03-09 08:18:48 +0000482 EmitAlignment(Align, 0);
483 EmitXXStructorList(GV->getInitializer());
484 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000485 }
486
487 return false;
488}
489
490/// EmitLLVMUsedList - For targets that define a TAI::UsedDirective, mark each
Dale Johannesen60567622008-09-09 22:29:13 +0000491/// global in the specified llvm.used list for which emitUsedDirectiveFor
492/// is true, as being used with this directive.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493void AsmPrinter::EmitLLVMUsedList(Constant *List) {
494 const char *Directive = TAI->getUsedDirective();
495
Dan Gohman9e1657f2009-06-14 23:30:43 +0000496 // Should be an array of 'i8*'.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
498 if (InitList == 0) return;
499
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner3f621b32009-07-17 22:00:23 +0000501 const GlobalValue *GV =
502 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner84362ac2009-07-31 20:52:39 +0000503 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen4911fb82008-09-03 20:34:58 +0000504 O << Directive;
505 EmitConstantValueOnly(InitList->getOperand(i));
506 O << '\n';
507 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508 }
509}
510
511/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
512/// function pointers, ignoring the init priority.
513void AsmPrinter::EmitXXStructorList(Constant *List) {
514 // Should be an array of '{ int, void ()* }' structs. The first value is the
515 // init priority, which we ignore.
516 if (!isa<ConstantArray>(List)) return;
517 ConstantArray *InitList = cast<ConstantArray>(List);
518 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
519 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
520 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
521
522 if (CS->getOperand(1)->isNullValue())
523 return; // Found a null terminator, exit printing.
524 // Emit the function pointer.
525 EmitGlobalConstant(CS->getOperand(1));
526 }
527}
528
529/// getGlobalLinkName - Returns the asm/link name of of the specified
530/// global variable. Should be overridden by each target asm printer to
531/// generate the appropriate value.
Bill Wendling26a8ab92009-04-10 00:12:49 +0000532const std::string &AsmPrinter::getGlobalLinkName(const GlobalVariable *GV,
533 std::string &LinkName) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000534 if (isa<Function>(GV)) {
535 LinkName += TAI->getFunctionAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000536 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 LinkName += TAI->getFunctionAddrSuffix();
538 } else {
539 LinkName += TAI->getGlobalVarAddrPrefix();
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000540 LinkName += Mang->getMangledName(GV);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 LinkName += TAI->getGlobalVarAddrSuffix();
542 }
543
544 return LinkName;
545}
546
547/// EmitExternalGlobal - Emit the external reference to a global variable.
548/// Should be overridden if an indirect reference should be used.
549void AsmPrinter::EmitExternalGlobal(const GlobalVariable *GV) {
Bill Wendling26a8ab92009-04-10 00:12:49 +0000550 std::string GLN;
551 O << getGlobalLinkName(GV, GLN);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552}
553
554
555
556//===----------------------------------------------------------------------===//
557/// LEB 128 number encoding.
558
559/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
560/// representing an unsigned leb128 value.
561void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000562 char Buffer[20];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000564 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 Value >>= 7;
566 if (Value) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000567 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000568 if (Value) O << ", ";
569 } while (Value);
570}
571
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000572/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
573/// representing a signed leb128 value.
574void AsmPrinter::PrintSLEB128(int Value) const {
575 int Sign = Value >> (8 * sizeof(Value) - 1);
576 bool IsMore;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000577 char Buffer[20];
aslc200b112008-08-16 12:57:46 +0000578
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 do {
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000580 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 Value >>= 7;
582 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
583 if (IsMore) Byte |= 0x80;
Chris Lattner83b7a2c2008-11-10 04:35:24 +0000584 O << "0x" << utohex_buffer(Byte, Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000585 if (IsMore) O << ", ";
586 } while (IsMore);
587}
588
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000589//===--------------------------------------------------------------------===//
590// Emission and print routines
591//
592
593/// PrintHex - Print a value as a hexidecimal value.
594///
595void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerda2047e2008-11-10 04:30:26 +0000596 char Buffer[20];
Chris Lattnerda2047e2008-11-10 04:30:26 +0000597 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000598}
599
600/// EOL - Print a newline character to asm stream. If a comment is present
601/// then it will be printed first. Comments should not contain '\n'.
602void AsmPrinter::EOL() const {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000603 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000604}
Owen Anderson367bfbb2008-07-01 21:16:27 +0000605
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000606void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000607 if (VerboseAsm && !Comment.empty()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000608 O << '\t'
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000609 << TAI->getCommentString()
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000610 << ' '
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000611 << Comment;
612 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000613 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000614}
615
Owen Anderson367bfbb2008-07-01 21:16:27 +0000616void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng0eeed442008-07-01 23:18:29 +0000617 if (VerboseAsm && *Comment) {
Owen Anderson367bfbb2008-07-01 21:16:27 +0000618 O << '\t'
619 << TAI->getCommentString()
620 << ' '
621 << Comment;
622 }
623 O << '\n';
624}
625
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000626/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
627/// unsigned leb128 value.
628void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
629 if (TAI->hasLEB128()) {
630 O << "\t.uleb128\t"
631 << Value;
632 } else {
633 O << TAI->getData8bitsDirective();
634 PrintULEB128(Value);
635 }
636}
637
638/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
639/// signed leb128 value.
640void AsmPrinter::EmitSLEB128Bytes(int Value) const {
641 if (TAI->hasLEB128()) {
642 O << "\t.sleb128\t"
643 << Value;
644 } else {
645 O << TAI->getData8bitsDirective();
646 PrintSLEB128(Value);
647 }
648}
649
650/// EmitInt8 - Emit a byte directive and value.
651///
652void AsmPrinter::EmitInt8(int Value) const {
653 O << TAI->getData8bitsDirective();
654 PrintHex(Value & 0xFF);
655}
656
657/// EmitInt16 - Emit a short directive and value.
658///
659void AsmPrinter::EmitInt16(int Value) const {
660 O << TAI->getData16bitsDirective();
661 PrintHex(Value & 0xFFFF);
662}
663
664/// EmitInt32 - Emit a long directive and value.
665///
666void AsmPrinter::EmitInt32(int Value) const {
667 O << TAI->getData32bitsDirective();
668 PrintHex(Value);
669}
670
671/// EmitInt64 - Emit a long long directive and value.
672///
673void AsmPrinter::EmitInt64(uint64_t Value) const {
674 if (TAI->getData64bitsDirective()) {
675 O << TAI->getData64bitsDirective();
676 PrintHex(Value);
677 } else {
678 if (TM.getTargetData()->isBigEndian()) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000679 EmitInt32(unsigned(Value >> 32)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000680 EmitInt32(unsigned(Value));
681 } else {
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000682 EmitInt32(unsigned(Value)); O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000683 EmitInt32(unsigned(Value >> 32));
684 }
685 }
686}
687
688/// toOctal - Convert the low order bits of X into an octal digit.
689///
690static inline char toOctal(int X) {
691 return (X&7)+'0';
692}
693
694/// printStringChar - Print a char, escaped if necessary.
695///
David Greene302008d2009-07-14 20:18:05 +0000696static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000697 if (C == '"') {
698 O << "\\\"";
699 } else if (C == '\\') {
700 O << "\\\\";
Chris Lattner07ec1462009-01-22 23:38:45 +0000701 } else if (isprint((unsigned char)C)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000702 O << C;
703 } else {
704 switch(C) {
705 case '\b': O << "\\b"; break;
706 case '\f': O << "\\f"; break;
707 case '\n': O << "\\n"; break;
708 case '\r': O << "\\r"; break;
709 case '\t': O << "\\t"; break;
710 default:
711 O << '\\';
712 O << toOctal(C >> 6);
713 O << toOctal(C >> 3);
714 O << toOctal(C >> 0);
715 break;
716 }
717 }
718}
719
720/// EmitString - Emit a string with quotes and a null terminator.
721/// Special characters are emitted properly.
722/// \literal (Eg. '\t') \endliteral
723void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendling3f94b412009-04-09 23:51:31 +0000724 EmitString(String.c_str(), String.size());
725}
726
727void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000728 const char* AscizDirective = TAI->getAscizDirective();
729 if (AscizDirective)
730 O << AscizDirective;
731 else
732 O << TAI->getAsciiDirective();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000733 O << '\"';
Bill Wendling3f94b412009-04-09 23:51:31 +0000734 for (unsigned i = 0; i < Size; ++i)
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000735 printStringChar(O, String[i]);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000736 if (AscizDirective)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000737 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000738 else
739 O << "\\0\"";
740}
741
742
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000743/// EmitFile - Emit a .file directive.
744void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
745 O << "\t.file\t" << Number << " \"";
Chris Lattnere2d4bf72009-04-08 00:28:38 +0000746 for (unsigned i = 0, N = Name.size(); i < N; ++i)
747 printStringChar(O, Name[i]);
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000748 O << '\"';
Dan Gohmane7ba1be2007-09-24 20:58:13 +0000749}
750
751
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000752//===----------------------------------------------------------------------===//
753
754// EmitAlignment - Emit an alignment directive to the specified power of
755// two boundary. For example, if you pass in 3 here, you will get an 8
756// byte alignment. If a global value is specified, and if that global has
757// an explicit alignment requested, it will unconditionally override the
758// alignment request. However, if ForcedAlignBits is specified, this value
759// has final say: the ultimate alignment will be the max of ForcedAlignBits
760// and the alignment computed with NumBits and the global.
761//
762// The algorithm is:
763// Align = NumBits;
764// if (GV && GV->hasalignment) Align = GV->getalignment();
765// Align = std::max(Align, ForcedAlignBits);
766//
767void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng7e7d1942008-02-29 19:36:59 +0000768 unsigned ForcedAlignBits,
769 bool UseFillExpr) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000770 if (GV && GV->getAlignment())
771 NumBits = Log2_32(GV->getAlignment());
772 NumBits = std::max(NumBits, ForcedAlignBits);
773
774 if (NumBits == 0) return; // No need to emit alignment.
775 if (TAI->getAlignmentIsInBytes()) NumBits = 1 << NumBits;
Evan Chengc1f41aa2007-07-25 23:35:07 +0000776 O << TAI->getAlignDirective() << NumBits;
Evan Cheng45c1edb2008-02-28 00:43:03 +0000777
Chris Lattnerebd055c2009-08-03 23:20:21 +0000778 if (CurrentSection && CurrentSection->getKind().isText())
779 if (unsigned FillValue = TAI->getTextAlignFillValue()) {
780 O << ',';
781 PrintHex(FillValue);
782 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000783 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000784}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000785
David Greene4b62f5b2009-07-31 21:57:10 +0000786/// PadToColumn - This gets called every time a tab is emitted. If
787/// column padding is turned on, we replace the tab with the
788/// appropriate amount of padding. If not, we replace the tab with a
789/// space, except for the first operand so that initial operands are
790/// always lined up by tabs.
791void AsmPrinter::PadToColumn(unsigned Operand) const {
792 if (TAI->getOperandColumn(Operand) > 0) {
793 O.PadToColumn(TAI->getOperandColumn(Operand), 1);
794 }
795 else {
796 if (Operand == 1) {
797 // Emit the tab after the mnemonic.
798 O << '\t';
799 }
800 else {
801 // Replace the tab with a space.
802 O << ' ';
803 }
804 }
805}
806
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000807/// EmitZeros - Emit a block of zeros.
808///
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000809void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000810 if (NumZeros) {
811 if (TAI->getZeroDirective()) {
812 O << TAI->getZeroDirective() << NumZeros;
813 if (TAI->getZeroDirectiveSuffix())
814 O << TAI->getZeroDirectiveSuffix();
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000815 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000816 } else {
817 for (; NumZeros; --NumZeros)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +0000818 O << TAI->getData8bitsDirective(AddrSpace) << "0\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000819 }
820 }
821}
822
823// Print out the specified constant, without a storage class. Only the
824// constants valid in constant expressions can occur here.
825void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
826 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000827 O << '0';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000828 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel2c1d0552008-06-03 06:18:19 +0000829 O << CI->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000830 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
831 // This is a constant address for a global variable or function. Use the
832 // name of the variable or function as the address value, possibly
833 // decorating it with GlobalVarAddrPrefix/Suffix or
834 // FunctionAddrPrefix/Suffix (these all default to "" )
835 if (isa<Function>(GV)) {
836 O << TAI->getFunctionAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000837 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000838 << TAI->getFunctionAddrSuffix();
839 } else {
840 O << TAI->getGlobalVarAddrPrefix()
Chris Lattnerb3cdde62009-07-14 18:17:16 +0000841 << Mang->getMangledName(GV)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000842 << TAI->getGlobalVarAddrSuffix();
843 }
844 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
845 const TargetData *TD = TM.getTargetData();
846 unsigned Opcode = CE->getOpcode();
847 switch (Opcode) {
Chris Lattner34e4cde2009-08-01 22:25:12 +0000848 case Instruction::Trunc:
849 case Instruction::ZExt:
850 case Instruction::SExt:
851 case Instruction::FPTrunc:
852 case Instruction::FPExt:
853 case Instruction::UIToFP:
854 case Instruction::SIToFP:
855 case Instruction::FPToUI:
856 case Instruction::FPToSI:
857 llvm_unreachable("FIXME: Don't support this constant cast expr");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000858 case Instruction::GetElementPtr: {
859 // generate a symbolic expression for the byte address
860 const Constant *ptrVal = CE->getOperand(0);
861 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
862 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
863 idxVec.size())) {
Chris Lattner911129a2009-02-05 06:55:21 +0000864 // Truncate/sext the offset to the pointer size.
865 if (TD->getPointerSizeInBits() != 64) {
866 int SExtAmount = 64-TD->getPointerSizeInBits();
867 Offset = (Offset << SExtAmount) >> SExtAmount;
868 }
869
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000870 if (Offset)
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000871 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000872 EmitConstantValueOnly(ptrVal);
873 if (Offset > 0)
874 O << ") + " << Offset;
875 else if (Offset < 0)
876 O << ") - " << -Offset;
877 } else {
878 EmitConstantValueOnly(ptrVal);
879 }
880 break;
881 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000882 case Instruction::BitCast:
883 return EmitConstantValueOnly(CE->getOperand(0));
884
885 case Instruction::IntToPtr: {
886 // Handle casts to pointers by changing them into casts to the appropriate
887 // integer type. This promotes constant folding and simplifies this code.
888 Constant *Op = CE->getOperand(0);
889 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(), false/*ZExt*/);
890 return EmitConstantValueOnly(Op);
891 }
892
893
894 case Instruction::PtrToInt: {
895 // Support only foldable casts to/from pointers that can be eliminated by
896 // changing the pointer to the appropriately sized integer type.
897 Constant *Op = CE->getOperand(0);
898 const Type *Ty = CE->getType();
899
900 // We can emit the pointer value into this slot if the slot is an
901 // integer slot greater or equal to the size of the pointer.
Chris Lattner34e4cde2009-08-01 22:25:12 +0000902 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000903 return EmitConstantValueOnly(Op);
Nick Lewycky95353ad2008-08-08 06:34:07 +0000904
905 O << "((";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000906 EmitConstantValueOnly(Op);
Chris Lattner34e4cde2009-08-01 22:25:12 +0000907 APInt ptrMask =
908 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattner89b36582008-08-17 07:19:36 +0000909
910 SmallString<40> S;
911 ptrMask.toStringUnsigned(S);
912 O << ") & " << S.c_str() << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000913 break;
914 }
915 case Instruction::Add:
916 case Instruction::Sub:
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000917 case Instruction::And:
918 case Instruction::Or:
919 case Instruction::Xor:
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000920 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000921 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000922 O << ')';
Anton Korobeynikovd3b58742007-12-18 20:53:41 +0000923 switch (Opcode) {
924 case Instruction::Add:
925 O << " + ";
926 break;
927 case Instruction::Sub:
928 O << " - ";
929 break;
930 case Instruction::And:
931 O << " & ";
932 break;
933 case Instruction::Or:
934 O << " | ";
935 break;
936 case Instruction::Xor:
937 O << " ^ ";
938 break;
939 default:
940 break;
941 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000942 O << '(';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000943 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000944 O << ')';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000945 break;
946 default:
Edwin Törökbd448e32009-07-14 16:55:14 +0000947 llvm_unreachable("Unsupported operator!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000948 }
949 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +0000950 llvm_unreachable("Unknown constant value!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000951 }
952}
953
954/// printAsCString - Print the specified array as a C compatible string, only if
955/// the predicate isString is true.
956///
David Greene302008d2009-07-14 20:18:05 +0000957static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000958 unsigned LastElt) {
959 assert(CVA->isString() && "Array is not string compatible!");
960
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000961 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000962 for (unsigned i = 0; i != LastElt; ++i) {
963 unsigned char C =
964 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
965 printStringChar(O, C);
966 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000967 O << '\"';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000968}
969
970/// EmitString - Emit a zero-byte-terminated string constant.
971///
972void AsmPrinter::EmitString(const ConstantArray *CVA) const {
973 unsigned NumElts = CVA->getNumOperands();
974 if (TAI->getAscizDirective() && NumElts &&
975 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
976 O << TAI->getAscizDirective();
977 printAsCString(O, CVA, NumElts-1);
978 } else {
979 O << TAI->getAsciiDirective();
980 printAsCString(O, CVA, NumElts);
981 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +0000982 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000983}
984
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000985void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
986 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +0000987 if (CVA->isString()) {
988 EmitString(CVA);
989 } else { // Not a string. Print the values in successive locations
990 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +0000991 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +0000992 }
993}
994
995void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
996 const VectorType *PTy = CP->getType();
997
998 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
999 EmitGlobalConstant(CP->getOperand(I));
1000}
1001
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001002void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
1003 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001004 // Print the fields in successive locations. Pad to align if needed!
1005 const TargetData *TD = TM.getTargetData();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001006 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +00001007 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
1008 uint64_t sizeSoFar = 0;
1009 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
1010 const Constant* field = CVS->getOperand(i);
1011
1012 // Check if padding is needed and insert one or more 0s.
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001013 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohmane78b0c72008-12-22 21:14:27 +00001014 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
1015 - cvsLayout->getElementOffset(i)) - fieldSize;
1016 sizeSoFar += fieldSize + padSize;
1017
1018 // Now print the actual field value.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001019 EmitGlobalConstant(field, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001020
1021 // Insert padding - this may include padding to increase the size of the
1022 // current field up to the ABI size (if the struct is not packed) as well
1023 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001024 EmitZeros(padSize, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001025 }
1026 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1027 "Layout of constant struct may be incorrect!");
1028}
1029
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001030void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1031 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001032 // FP Constants are printed as integer constants to avoid losing
1033 // precision...
1034 const TargetData *TD = TM.getTargetData();
1035 if (CFP->getType() == Type::DoubleTy) {
1036 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1037 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Evan Cheng11db8142009-03-24 00:17:40 +00001038 if (TAI->getData64bitsDirective(AddrSpace)) {
1039 O << TAI->getData64bitsDirective(AddrSpace) << i;
1040 if (VerboseAsm)
1041 O << '\t' << TAI->getCommentString() << " double value: " << Val;
1042 O << '\n';
1043 } else if (TD->isBigEndian()) {
1044 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1045 if (VerboseAsm)
1046 O << '\t' << TAI->getCommentString()
1047 << " double most significant word " << Val;
1048 O << '\n';
1049 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1050 if (VerboseAsm)
1051 O << '\t' << TAI->getCommentString()
1052 << " double least significant word " << Val;
1053 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001054 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001055 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i);
1056 if (VerboseAsm)
1057 O << '\t' << TAI->getCommentString()
1058 << " double least significant word " << Val;
1059 O << '\n';
1060 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
1061 if (VerboseAsm)
1062 O << '\t' << TAI->getCommentString()
1063 << " double most significant word " << Val;
1064 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001065 }
1066 return;
1067 } else if (CFP->getType() == Type::FloatTy) {
1068 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001069 O << TAI->getData32bitsDirective(AddrSpace)
Evan Cheng11db8142009-03-24 00:17:40 +00001070 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1071 if (VerboseAsm)
1072 O << '\t' << TAI->getCommentString() << " float " << Val;
1073 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001074 return;
1075 } else if (CFP->getType() == Type::X86_FP80Ty) {
1076 // all long double variants are printed as hex
1077 // api needed to prevent premature destruction
1078 APInt api = CFP->getValueAPF().bitcastToAPInt();
1079 const uint64_t *p = api.getRawData();
1080 // Convert to double so we can print the approximate val as a comment.
1081 APFloat DoubleVal = CFP->getValueAPF();
1082 bool ignored;
1083 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1084 &ignored);
1085 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001086 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1087 if (VerboseAsm)
1088 O << '\t' << TAI->getCommentString()
1089 << " long double most significant halfword of ~"
1090 << DoubleVal.convertToDouble();
1091 O << '\n';
1092 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1093 if (VerboseAsm)
1094 O << '\t' << TAI->getCommentString() << " long double next halfword";
1095 O << '\n';
1096 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1097 if (VerboseAsm)
1098 O << '\t' << TAI->getCommentString() << " long double next halfword";
1099 O << '\n';
1100 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1101 if (VerboseAsm)
1102 O << '\t' << TAI->getCommentString() << " long double next halfword";
1103 O << '\n';
1104 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1105 if (VerboseAsm)
1106 O << '\t' << TAI->getCommentString()
1107 << " long double least significant halfword";
1108 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001109 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001110 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
1111 if (VerboseAsm)
1112 O << '\t' << TAI->getCommentString()
1113 << " long double least significant halfword of ~"
1114 << DoubleVal.convertToDouble();
1115 O << '\n';
1116 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
1117 if (VerboseAsm)
1118 O << '\t' << TAI->getCommentString()
1119 << " long double next halfword";
1120 O << '\n';
1121 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
1122 if (VerboseAsm)
1123 O << '\t' << TAI->getCommentString()
1124 << " long double next halfword";
1125 O << '\n';
1126 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
1127 if (VerboseAsm)
1128 O << '\t' << TAI->getCommentString()
1129 << " long double next halfword";
1130 O << '\n';
1131 O << TAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
1132 if (VerboseAsm)
1133 O << '\t' << TAI->getCommentString()
1134 << " long double most significant halfword";
1135 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001136 }
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001137 EmitZeros(TD->getTypeAllocSize(Type::X86_FP80Ty) -
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001138 TD->getTypeStoreSize(Type::X86_FP80Ty), AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001139 return;
1140 } else if (CFP->getType() == Type::PPC_FP128Ty) {
1141 // all long double variants are printed as hex
1142 // api needed to prevent premature destruction
1143 APInt api = CFP->getValueAPF().bitcastToAPInt();
1144 const uint64_t *p = api.getRawData();
1145 if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001146 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1147 if (VerboseAsm)
1148 O << '\t' << TAI->getCommentString()
1149 << " long double most significant word";
1150 O << '\n';
1151 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1152 if (VerboseAsm)
1153 O << '\t' << TAI->getCommentString()
1154 << " long double next word";
1155 O << '\n';
1156 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1157 if (VerboseAsm)
1158 O << '\t' << TAI->getCommentString()
1159 << " long double next word";
1160 O << '\n';
1161 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1162 if (VerboseAsm)
1163 O << '\t' << TAI->getCommentString()
1164 << " long double least significant word";
1165 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001166 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001167 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
1168 if (VerboseAsm)
1169 O << '\t' << TAI->getCommentString()
1170 << " long double least significant word";
1171 O << '\n';
1172 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
1173 if (VerboseAsm)
1174 O << '\t' << TAI->getCommentString()
1175 << " long double next word";
1176 O << '\n';
1177 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
1178 if (VerboseAsm)
1179 O << '\t' << TAI->getCommentString()
1180 << " long double next word";
1181 O << '\n';
1182 O << TAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
1183 if (VerboseAsm)
1184 O << '\t' << TAI->getCommentString()
1185 << " long double most significant word";
1186 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001187 }
1188 return;
Edwin Törökbd448e32009-07-14 16:55:14 +00001189 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohmane78b0c72008-12-22 21:14:27 +00001190}
1191
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001192void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1193 unsigned AddrSpace) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001194 const TargetData *TD = TM.getTargetData();
1195 unsigned BitWidth = CI->getBitWidth();
1196 assert(isPowerOf2_32(BitWidth) &&
1197 "Non-power-of-2-sized integers not handled!");
1198
1199 // We don't expect assemblers to support integer data directives
1200 // for more than 64 bits, so we emit the data in at most 64-bit
1201 // quantities at a time.
1202 const uint64_t *RawData = CI->getValue().getRawData();
1203 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1204 uint64_t Val;
1205 if (TD->isBigEndian())
1206 Val = RawData[e - i - 1];
1207 else
1208 Val = RawData[i];
1209
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001210 if (TAI->getData64bitsDirective(AddrSpace))
1211 O << TAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001212 else if (TD->isBigEndian()) {
Evan Cheng11db8142009-03-24 00:17:40 +00001213 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1214 if (VerboseAsm)
1215 O << '\t' << TAI->getCommentString()
1216 << " Double-word most significant word " << Val;
1217 O << '\n';
1218 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1219 if (VerboseAsm)
1220 O << '\t' << TAI->getCommentString()
1221 << " Double-word least significant word " << Val;
1222 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001223 } else {
Evan Cheng11db8142009-03-24 00:17:40 +00001224 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
1225 if (VerboseAsm)
1226 O << '\t' << TAI->getCommentString()
1227 << " Double-word least significant word " << Val;
1228 O << '\n';
1229 O << TAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
1230 if (VerboseAsm)
1231 O << '\t' << TAI->getCommentString()
1232 << " Double-word most significant word " << Val;
1233 O << '\n';
Dan Gohmane78b0c72008-12-22 21:14:27 +00001234 }
1235 }
1236}
1237
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001238/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001239void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001240 const TargetData *TD = TM.getTargetData();
Dan Gohmane78b0c72008-12-22 21:14:27 +00001241 const Type *type = CV->getType();
Duncan Sandsec4f97d2009-05-09 07:06:46 +00001242 unsigned Size = TD->getTypeAllocSize(type);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001243
1244 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001245 EmitZeros(Size, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246 return;
1247 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptac1f8d9c2009-04-28 16:34:20 +00001248 EmitGlobalConstantArray(CVA , AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001249 return;
1250 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001251 EmitGlobalConstantStruct(CVS, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001252 return;
1253 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001254 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohmane78b0c72008-12-22 21:14:27 +00001255 return;
1256 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1257 // Small integers are handled below; large integers are handled here.
1258 if (Size > 4) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001259 EmitGlobalConstantLargeInt(CI, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260 return;
1261 }
1262 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohmane78b0c72008-12-22 21:14:27 +00001263 EmitGlobalConstantVector(CP);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001264 return;
1265 }
1266
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001267 printDataDirective(type, AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 EmitConstantValueOnly(CV);
Evan Cheng11db8142009-03-24 00:17:40 +00001269 if (VerboseAsm) {
1270 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1271 SmallString<40> S;
1272 CI->getValue().toStringUnsigned(S, 16);
1273 O << "\t\t\t" << TAI->getCommentString() << " 0x" << S.c_str();
1274 }
Scott Michele067c3c2008-06-03 15:39:51 +00001275 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001276 O << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001277}
1278
Chris Lattner89b36582008-08-17 07:19:36 +00001279void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001280 // Target doesn't support this yet!
Edwin Törökbd448e32009-07-14 16:55:14 +00001281 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282}
1283
1284/// PrintSpecial - Print information related to the specified machine instr
1285/// that is independent of the operand, and may be independent of the instr
1286/// itself. This can be useful for portably encoding the comment character
1287/// or other bits of target-specific knowledge into the asmstrings. The
1288/// syntax used is ${:comment}. Targets can override this to add support
1289/// for their own strange codes.
Chris Lattner6af48032009-03-10 05:37:13 +00001290void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001291 if (!strcmp(Code, "private")) {
1292 O << TAI->getPrivateGlobalPrefix();
1293 } else if (!strcmp(Code, "comment")) {
Evan Cheng11db8142009-03-24 00:17:40 +00001294 if (VerboseAsm)
1295 O << TAI->getCommentString();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 } else if (!strcmp(Code, "uid")) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001297 // Comparing the address of MI isn't sufficient, because machineinstrs may
1298 // be allocated to the same address across functions.
1299 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1300
Owen Andersonda3388b2009-06-24 22:28:12 +00001301 // If this is a new LastFn instruction, bump the counter.
1302 if (LastMI != MI || LastFn != ThisF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001303 ++Counter;
1304 LastMI = MI;
Owen Andersonda3388b2009-06-24 22:28:12 +00001305 LastFn = ThisF;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001306 }
1307 O << Counter;
1308 } else {
Edwin Törökced9ff82009-07-11 13:10:19 +00001309 std::string msg;
1310 raw_string_ostream Msg(msg);
1311 Msg << "Unknown special formatter '" << Code
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 << "' for machine instr: " << *MI;
Edwin Törökced9ff82009-07-11 13:10:19 +00001313 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001314 }
1315}
1316
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001317/// processDebugLoc - Processes the debug information of each machine
1318/// instruction's DebugLoc.
1319void AsmPrinter::processDebugLoc(DebugLoc DL) {
Chris Lattner693cfcc2009-08-05 04:09:18 +00001320 if (!TAI || !DW)
1321 return;
1322
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001323 if (TAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
1324 if (!DL.isUnknown()) {
Argiris Kirtzidis3f997f82009-05-07 13:55:51 +00001325 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
1326
1327 if (CurDLT.CompileUnit != 0 && PrevDLT != CurDLT)
1328 printLabel(DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
1329 DICompileUnit(CurDLT.CompileUnit)));
1330
1331 PrevDLT = CurDLT;
1332 }
1333 }
1334}
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001335
1336/// printInlineAsm - This method formats and prints the specified machine
1337/// instruction that is an inline asm.
1338void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
1339 unsigned NumOperands = MI->getNumOperands();
1340
1341 // Count the number of register definitions.
1342 unsigned NumDefs = 0;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001343 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001344 ++NumDefs)
1345 assert(NumDefs != NumOperands-1 && "No asm string?");
1346
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001347 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001348
1349 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
1350 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1351
Dale Johannesene99fc902008-01-29 02:21:21 +00001352 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1353 // These are useful to see where empty asm's wound up.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001354 if (AsmStr[0] == 0) {
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001355 O << TAI->getInlineAsmStart() << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001356 return;
1357 }
1358
1359 O << TAI->getInlineAsmStart() << "\n\t";
1360
1361 // The variant of the current asmprinter.
1362 int AsmPrinterVariant = TAI->getAssemblerDialect();
1363
1364 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1365 const char *LastEmitted = AsmStr; // One past the last character emitted.
1366
1367 while (*LastEmitted) {
1368 switch (*LastEmitted) {
1369 default: {
1370 // Not a special case, emit the string section literally.
1371 const char *LiteralEnd = LastEmitted+1;
1372 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
1373 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
1374 ++LiteralEnd;
1375 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1376 O.write(LastEmitted, LiteralEnd-LastEmitted);
1377 LastEmitted = LiteralEnd;
1378 break;
1379 }
1380 case '\n':
1381 ++LastEmitted; // Consume newline character.
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001382 O << '\n'; // Indent code with newline.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001383 break;
1384 case '$': {
1385 ++LastEmitted; // Consume '$' character.
1386 bool Done = true;
1387
1388 // Handle escapes.
1389 switch (*LastEmitted) {
1390 default: Done = false; break;
1391 case '$': // $$ -> $
1392 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1393 O << '$';
1394 ++LastEmitted; // Consume second '$' character.
1395 break;
1396 case '(': // $( -> same as GCC's { character.
1397 ++LastEmitted; // Consume '(' character.
1398 if (CurVariant != -1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001399 llvm_report_error("Nested variants found in inline asm string: '"
1400 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001401 }
1402 CurVariant = 0; // We're in the first variant now.
1403 break;
1404 case '|':
1405 ++LastEmitted; // consume '|' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001406 if (CurVariant == -1)
1407 O << '|'; // this is gcc's behavior for | outside a variant
1408 else
1409 ++CurVariant; // We're in the next variant.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001410 break;
1411 case ')': // $) -> same as GCC's } char.
1412 ++LastEmitted; // consume ')' character.
Dale Johannesen8b0e1172008-10-10 21:04:42 +00001413 if (CurVariant == -1)
1414 O << '}'; // this is gcc's behavior for } outside a variant
1415 else
1416 CurVariant = -1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001417 break;
1418 }
1419 if (Done) break;
1420
1421 bool HasCurlyBraces = false;
1422 if (*LastEmitted == '{') { // ${variable}
1423 ++LastEmitted; // Consume '{' character.
1424 HasCurlyBraces = true;
1425 }
1426
Chris Lattner6af48032009-03-10 05:37:13 +00001427 // If we have ${:foo}, then this is not a real operand reference, it is a
1428 // "magic" string reference, just like in .td files. Arrange to call
1429 // PrintSpecial.
1430 if (HasCurlyBraces && *LastEmitted == ':') {
1431 ++LastEmitted;
1432 const char *StrStart = LastEmitted;
1433 const char *StrEnd = strchr(StrStart, '}');
1434 if (StrEnd == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001435 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1436 + std::string(AsmStr) + "'");
Chris Lattner6af48032009-03-10 05:37:13 +00001437 }
1438
1439 std::string Val(StrStart, StrEnd);
1440 PrintSpecial(MI, Val.c_str());
1441 LastEmitted = StrEnd+1;
1442 break;
1443 }
1444
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001445 const char *IDStart = LastEmitted;
1446 char *IDEnd;
1447 errno = 0;
1448 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1449 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001450 llvm_report_error("Bad $ operand number in inline asm string: '"
1451 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001452 }
1453 LastEmitted = IDEnd;
1454
1455 char Modifier[2] = { 0, 0 };
1456
1457 if (HasCurlyBraces) {
1458 // If we have curly braces, check for a modifier character. This
1459 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1460 if (*LastEmitted == ':') {
1461 ++LastEmitted; // Consume ':' character.
1462 if (*LastEmitted == 0) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001463 llvm_report_error("Bad ${:} expression in inline asm string: '"
1464 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001465 }
1466
1467 Modifier[0] = *LastEmitted;
1468 ++LastEmitted; // Consume modifier character.
1469 }
1470
1471 if (*LastEmitted != '}') {
Edwin Törökced9ff82009-07-11 13:10:19 +00001472 llvm_report_error("Bad ${} expression in inline asm string: '"
1473 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001474 }
1475 ++LastEmitted; // Consume '}' character.
1476 }
1477
1478 if ((unsigned)Val >= NumOperands-1) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001479 llvm_report_error("Invalid $ operand number in inline asm string: '"
1480 + std::string(AsmStr) + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001481 }
1482
1483 // Okay, we finally have a value number. Ask the target to print this
1484 // operand!
1485 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1486 unsigned OpNo = 1;
1487
1488 bool Error = false;
1489
1490 // Scan to find the machine operand number for the operand.
1491 for (; Val; --Val) {
1492 if (OpNo >= MI->getNumOperands()) break;
Chris Lattnerda4cff12007-12-30 20:50:28 +00001493 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng92167402009-03-20 18:03:34 +00001494 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001495 }
1496
1497 if (OpNo >= MI->getNumOperands()) {
1498 Error = true;
1499 } else {
Chris Lattnerda4cff12007-12-30 20:50:28 +00001500 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001501 ++OpNo; // Skip over the ID number.
1502
Dale Johannesencfb19e62007-11-05 21:20:28 +00001503 if (Modifier[0]=='l') // labels are target independent
Chris Lattner6017d482007-12-30 23:10:15 +00001504 printBasicBlockLabel(MI->getOperand(OpNo).getMBB(),
Evan Cheng45c1edb2008-02-28 00:43:03 +00001505 false, false, false);
Dale Johannesencfb19e62007-11-05 21:20:28 +00001506 else {
1507 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen94464072008-09-24 01:07:17 +00001508 if ((OpFlags & 7) == 4) {
Dale Johannesencfb19e62007-11-05 21:20:28 +00001509 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1510 Modifier[0] ? Modifier : 0);
1511 } else {
1512 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1513 Modifier[0] ? Modifier : 0);
1514 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001515 }
1516 }
1517 if (Error) {
Edwin Törökced9ff82009-07-11 13:10:19 +00001518 std::string msg;
1519 raw_string_ostream Msg(msg);
1520 Msg << "Invalid operand found in inline asm: '"
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001521 << AsmStr << "'\n";
Edwin Törökced9ff82009-07-11 13:10:19 +00001522 MI->print(Msg);
1523 llvm_report_error(Msg.str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001524 }
1525 }
1526 break;
1527 }
1528 }
1529 }
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001530 O << "\n\t" << TAI->getInlineAsmEnd() << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001531}
1532
Evan Cheng3c0eda52008-03-15 00:03:38 +00001533/// printImplicitDef - This method prints the specified machine instruction
1534/// that is an implicit def.
1535void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Evan Cheng11db8142009-03-24 00:17:40 +00001536 if (VerboseAsm)
1537 O << '\t' << TAI->getCommentString() << " implicit-def: "
1538 << TRI->getAsmName(MI->getOperand(0).getReg()) << '\n';
Evan Cheng3c0eda52008-03-15 00:03:38 +00001539}
1540
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001541/// printLabel - This method prints a local label used by debug and
1542/// exception handling tables.
1543void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman7d546402008-07-01 00:16:26 +00001544 printLabel(MI->getOperand(0).getImm());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001545}
1546
Evan Chenga53c40a2008-02-01 09:10:45 +00001547void AsmPrinter::printLabel(unsigned Id) const {
Evan Cheng8b988692008-02-02 08:39:46 +00001548 O << TAI->getPrivateGlobalPrefix() << "label" << Id << ":\n";
Evan Chenga53c40a2008-02-01 09:10:45 +00001549}
1550
Evan Cheng2e28d622008-02-02 04:07:54 +00001551/// printDeclare - This method prints a local variable declaration used by
1552/// debug tables.
Evan Chengc439a852008-02-04 23:06:48 +00001553/// FIXME: It doesn't really print anything rather it inserts a DebugVariable
1554/// entry into dwarf table.
Evan Cheng2e28d622008-02-02 04:07:54 +00001555void AsmPrinter::printDeclare(const MachineInstr *MI) const {
Devang Patelfe359e72009-01-13 21:44:10 +00001556 unsigned FI = MI->getOperand(0).getIndex();
Evan Chengc439a852008-02-04 23:06:48 +00001557 GlobalValue *GV = MI->getOperand(1).getGlobal();
Devang Patel8a9a7dc2009-04-15 00:10:26 +00001558 DW->RecordVariable(cast<GlobalVariable>(GV), FI, MI);
Evan Cheng2e28d622008-02-02 04:07:54 +00001559}
1560
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001561/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1562/// instruction, using the specified assembler variant. Targets should
1563/// overried this to format as appropriate.
1564bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1565 unsigned AsmVariant, const char *ExtraCode) {
1566 // Target doesn't support this yet!
1567 return true;
1568}
1569
1570bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1571 unsigned AsmVariant,
1572 const char *ExtraCode) {
1573 // Target doesn't support this yet!
1574 return true;
1575}
1576
1577/// printBasicBlockLabel - This method prints the label for the specified
1578/// MachineBasicBlock
1579void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
Evan Cheng45c1edb2008-02-28 00:43:03 +00001580 bool printAlign,
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001581 bool printColon,
1582 bool printComment) const {
Evan Cheng45c1edb2008-02-28 00:43:03 +00001583 if (printAlign) {
1584 unsigned Align = MBB->getAlignment();
1585 if (Align)
1586 EmitAlignment(Log2_32(Align));
1587 }
1588
Dan Gohman12ebe3f2008-06-30 22:03:41 +00001589 O << TAI->getPrivateGlobalPrefix() << "BB" << getFunctionNumber() << '_'
Evan Cheng477013c2007-10-14 05:57:21 +00001590 << MBB->getNumber();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001591 if (printColon)
1592 O << ':';
1593 if (printComment && MBB->getBasicBlock())
Dan Gohman0912cda2007-07-30 15:06:25 +00001594 O << '\t' << TAI->getCommentString() << ' '
Daniel Dunbar5d3ea962009-07-26 09:48:23 +00001595 << MBB->getBasicBlock()->getNameStr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001596}
1597
Evan Cheng6fb06762007-11-09 01:32:10 +00001598/// printPICJumpTableSetLabel - This method prints a set label for the
1599/// specified MachineBasicBlock for a jumptable entry.
1600void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1601 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001602 if (!TAI->getSetDirective())
1603 return;
1604
1605 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001606 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001607 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001608 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1609 << '_' << uid << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001610}
1611
Evan Cheng6fb06762007-11-09 01:32:10 +00001612void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1613 const MachineBasicBlock *MBB) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001614 if (!TAI->getSetDirective())
1615 return;
1616
1617 O << TAI->getSetDirective() << ' ' << TAI->getPrivateGlobalPrefix()
Evan Cheng477013c2007-10-14 05:57:21 +00001618 << getFunctionNumber() << '_' << uid << '_' << uid2
1619 << "_set_" << MBB->getNumber() << ',';
Evan Cheng45c1edb2008-02-28 00:43:03 +00001620 printBasicBlockLabel(MBB, false, false, false);
Evan Cheng477013c2007-10-14 05:57:21 +00001621 O << '-' << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
1622 << '_' << uid << '_' << uid2 << '\n';
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001623}
1624
1625/// printDataDirective - This method prints the asm directive for the
1626/// specified type.
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001627void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 const TargetData *TD = TM.getTargetData();
1629 switch (type->getTypeID()) {
Chris Lattner1ad1c1d2009-07-15 04:42:49 +00001630 case Type::FloatTyID: case Type::DoubleTyID:
1631 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1632 assert(0 && "Should have already output floating point constant.");
1633 default:
1634 assert(0 && "Can't handle printing this type of thing");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001635 case Type::IntegerTyID: {
1636 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1637 if (BitWidth <= 8)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001638 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001639 else if (BitWidth <= 16)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001640 O << TAI->getData16bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001641 else if (BitWidth <= 32)
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001642 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001643 else if (BitWidth <= 64) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001644 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001645 "Target cannot handle 64-bit constant exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001646 O << TAI->getData64bitsDirective(AddrSpace);
Dan Gohman07a91ea2008-09-08 16:40:13 +00001647 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00001648 llvm_unreachable("Target cannot handle given data directive width!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001649 }
1650 break;
1651 }
1652 case Type::PointerTyID:
1653 if (TD->getPointerSize() == 8) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001654 assert(TAI->getData64bitsDirective(AddrSpace) &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655 "Target cannot handle 64-bit pointer exprs!");
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001656 O << TAI->getData64bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001657 } else if (TD->getPointerSize() == 2) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001658 O << TAI->getData16bitsDirective(AddrSpace);
Sanjiv Gupta8a44cfb2009-01-22 10:14:21 +00001659 } else if (TD->getPointerSize() == 1) {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001660 O << TAI->getData8bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001661 } else {
Sanjiv Guptadc2943d2009-01-30 04:25:10 +00001662 O << TAI->getData32bitsDirective(AddrSpace);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001663 }
1664 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665 }
1666}
1667
Anton Korobeynikov78d69aa2008-08-08 18:25:07 +00001668void AsmPrinter::printVisibility(const std::string& Name,
1669 unsigned Visibility) const {
1670 if (Visibility == GlobalValue::HiddenVisibility) {
1671 if (const char *Directive = TAI->getHiddenDirective())
1672 O << Directive << Name << '\n';
1673 } else if (Visibility == GlobalValue::ProtectedVisibility) {
1674 if (const char *Directive = TAI->getProtectedDirective())
1675 O << Directive << Name << '\n';
1676 }
1677}
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001678
Anton Korobeynikov440f23d2008-11-22 16:15:34 +00001679void AsmPrinter::printOffset(int64_t Offset) const {
1680 if (Offset > 0)
1681 O << '+' << Offset;
1682 else if (Offset < 0)
1683 O << Offset;
1684}
1685
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001686GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1687 if (!S->usesMetadata())
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001688 return 0;
1689
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001690 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001691 if (GCPI != GCMetadataPrinters.end())
1692 return GCPI->second;
1693
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001694 const char *Name = S->getName().c_str();
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001695
1696 for (GCMetadataPrinterRegistry::iterator
1697 I = GCMetadataPrinterRegistry::begin(),
1698 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1699 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001700 GCMetadataPrinter *GMP = I->instantiate();
1701 GMP->S = S;
1702 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1703 return GMP;
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001704 }
1705
Gordon Henriksen1aed5992008-08-17 18:44:35 +00001706 cerr << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Edwin Törökbd448e32009-07-14 16:55:14 +00001707 llvm_unreachable(0);
Gordon Henriksen3385c9b2008-08-17 12:08:44 +00001708}
David Greene63486122009-07-13 20:25:48 +00001709
1710/// EmitComments - Pretty-print comments for instructions
1711void AsmPrinter::EmitComments(const MachineInstr &MI) const
1712{
David Greene1fa35942009-07-22 20:33:26 +00001713 if (VerboseAsm) {
1714 if (!MI.getDebugLoc().isUnknown()) {
1715 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001716
David Greene1fa35942009-07-22 20:33:26 +00001717 // Print source line info
1718 O.PadToColumn(TAI->getCommentColumn(), 1);
1719 O << TAI->getCommentString() << " SrcLine ";
1720 if (DLT.CompileUnit->hasInitializer()) {
1721 Constant *Name = DLT.CompileUnit->getInitializer();
1722 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1723 if (NameString->isString()) {
1724 O << NameString->getAsString() << " ";
1725 }
1726 }
1727 O << DLT.Line;
1728 if (DLT.Col != 0)
1729 O << ":" << DLT.Col;
1730 }
David Greene4a37a692009-07-16 22:24:20 +00001731 }
David Greene63486122009-07-13 20:25:48 +00001732}
1733
1734/// EmitComments - Pretty-print comments for instructions
1735void AsmPrinter::EmitComments(const MCInst &MI) const
1736{
David Greene1fa35942009-07-22 20:33:26 +00001737 if (VerboseAsm) {
1738 if (!MI.getDebugLoc().isUnknown()) {
1739 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene4a37a692009-07-16 22:24:20 +00001740
David Greene1fa35942009-07-22 20:33:26 +00001741 // Print source line info
1742 O.PadToColumn(TAI->getCommentColumn(), 1);
1743 O << TAI->getCommentString() << " SrcLine ";
1744 if (DLT.CompileUnit->hasInitializer()) {
1745 Constant *Name = DLT.CompileUnit->getInitializer();
1746 if (ConstantArray *NameString = dyn_cast<ConstantArray>(Name))
1747 if (NameString->isString()) {
1748 O << NameString->getAsString() << " ";
1749 }
1750 }
1751 O << DLT.Line;
1752 if (DLT.Col != 0)
1753 O << ":" << DLT.Col;
1754 }
David Greene4a37a692009-07-16 22:24:20 +00001755 }
David Greene63486122009-07-13 20:25:48 +00001756}