blob: bb6bd957f063bddb15cb12620c71e20caa1be2ed [file] [log] [blame]
Chris Lattnera80ba712004-08-16 23:15:22 +00001//===-- AsmPrinter.cpp - Common AsmPrinter code ---------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera80ba712004-08-16 23:15:22 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the AsmPrinter class.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng932f0222006-03-03 02:04:29 +000014#include "llvm/CodeGen/AsmPrinter.h"
Evan Cheng246ae0d2006-03-01 22:18:09 +000015#include "llvm/Assembly/Writer.h"
Nate Begeman8cfa57b2005-12-06 06:18:55 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000017#include "llvm/Constants.h"
Chris Lattner450de392005-11-10 18:36:17 +000018#include "llvm/Module.h"
Gordon Henriksen5eca0752008-08-17 18:44:35 +000019#include "llvm/CodeGen/GCMetadataPrinter.h"
Chris Lattner3b4fd322005-11-21 08:25:09 +000020#include "llvm/CodeGen/MachineConstantPool.h"
David Greenefe37ab32009-08-18 19:22:55 +000021#include "llvm/CodeGen/MachineFunction.h"
Nate Begeman37efe672006-04-22 18:53:45 +000022#include "llvm/CodeGen/MachineJumpTableInfo.h"
David Greeneb71d1b22009-08-10 16:38:07 +000023#include "llvm/CodeGen/MachineLoopInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000024#include "llvm/CodeGen/MachineModuleInfo.h"
Devang Patelc48c5502009-01-13 21:44:10 +000025#include "llvm/CodeGen/DwarfWriter.h"
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +000026#include "llvm/Analysis/DebugInfo.h"
Chris Lattner2b2954f2009-07-27 21:28:04 +000027#include "llvm/MC/MCContext.h"
David Greene3ac1ab82009-07-16 22:24:20 +000028#include "llvm/MC/MCInst.h"
Chris Lattnera87dea42009-07-31 18:48:30 +000029#include "llvm/MC/MCSection.h"
30#include "llvm/MC/MCStreamer.h"
Chris Lattner7cb384d2009-09-12 23:02:08 +000031#include "llvm/MC/MCSymbol.h"
Evan Cheng42bf74b2009-03-25 01:47:28 +000032#include "llvm/Support/CommandLine.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000033#include "llvm/Support/ErrorHandling.h"
David Greene014700c2009-07-13 20:25:48 +000034#include "llvm/Support/FormattedStream.h"
Chris Lattnera80ba712004-08-16 23:15:22 +000035#include "llvm/Support/Mangler.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
Owen Anderson07000c62006-05-12 06:33:49 +000037#include "llvm/Target/TargetData.h"
Chris Lattner0336fdb2006-10-06 22:50:56 +000038#include "llvm/Target/TargetLowering.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Cheng6547e402008-07-01 23:18:29 +000040#include "llvm/Target/TargetOptions.h"
Evan Chengda47e6e2008-03-15 00:03:38 +000041#include "llvm/Target/TargetRegisterInfo.h"
Evan Chengcc415862007-11-09 01:32:10 +000042#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerfad86b02008-08-17 07:19:36 +000043#include "llvm/ADT/SmallString.h"
Owen Andersoncb371882008-08-21 00:14:44 +000044#include "llvm/ADT/StringExtras.h"
Chris Lattner66099132006-02-01 22:41:11 +000045#include <cerrno>
Chris Lattnera80ba712004-08-16 23:15:22 +000046using namespace llvm;
47
Evan Cheng42bf74b2009-03-25 01:47:28 +000048static cl::opt<cl::boolOrDefault>
49AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
50 cl::init(cl::BOU_UNSET));
51
Devang Patel19974732007-05-03 01:11:54 +000052char AsmPrinter::ID = 0;
David Greene71847812009-07-14 20:18:05 +000053AsmPrinter::AsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
Chris Lattneraf76e592009-08-22 20:48:53 +000054 const MCAsmInfo *T, bool VDef)
Daniel Dunbar5bcc8bd2009-07-01 01:48:54 +000055 : MachineFunctionPass(&ID), FunctionNumber(0), O(o),
Chris Lattner33adcfb2009-08-22 21:43:10 +000056 TM(tm), MAI(T), TRI(tm.getRegisterInfo()),
Chris Lattner2b2954f2009-07-27 21:28:04 +000057
58 OutContext(*new MCContext()),
Chris Lattner90edac02009-09-14 03:02:37 +000059 // FIXME: Pass instprinter to streamer.
60 OutStreamer(*createAsmStreamer(OutContext, O, *T, 0)),
Chris Lattner2b2954f2009-07-27 21:28:04 +000061
Chris Lattner290c2f52009-08-03 23:20:21 +000062 LastMI(0), LastFn(0), Counter(~0U),
Devang Patela1434042009-10-01 01:15:28 +000063 PrevDLT(0, 0, ~0U, ~0U) {
Chris Lattner0de1fc42009-06-24 19:09:55 +000064 DW = 0; MMI = 0;
Evan Cheng42bf74b2009-03-25 01:47:28 +000065 switch (AsmVerbose) {
66 case cl::BOU_UNSET: VerboseAsm = VDef; break;
67 case cl::BOU_TRUE: VerboseAsm = true; break;
68 case cl::BOU_FALSE: VerboseAsm = false; break;
69 }
70}
Chris Lattnered138932005-12-13 06:32:10 +000071
Gordon Henriksenc317a602008-08-17 12:08:44 +000072AsmPrinter::~AsmPrinter() {
73 for (gcp_iterator I = GCMetadataPrinters.begin(),
74 E = GCMetadataPrinters.end(); I != E; ++I)
75 delete I->second;
Chris Lattner2b2954f2009-07-27 21:28:04 +000076
77 delete &OutStreamer;
78 delete &OutContext;
Gordon Henriksenc317a602008-08-17 12:08:44 +000079}
Chris Lattnered138932005-12-13 06:32:10 +000080
Chris Lattner38c39882009-08-03 19:12:26 +000081TargetLoweringObjectFile &AsmPrinter::getObjFileLowering() const {
Chris Lattnerf0144122009-07-28 03:13:23 +000082 return TM.getTargetLowering()->getObjFileLowering();
83}
84
Chris Lattnerdabf07c2009-08-18 06:15:16 +000085/// getCurrentSection() - Return the current section we are emitting to.
86const MCSection *AsmPrinter::getCurrentSection() const {
87 return OutStreamer.getCurrentSection();
88}
89
90
Gordon Henriksence224772008-01-07 01:30:38 +000091void AsmPrinter::getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman845012e2009-07-31 23:37:33 +000092 AU.setPreservesAll();
Gordon Henriksence224772008-01-07 01:30:38 +000093 MachineFunctionPass::getAnalysisUsage(AU);
Gordon Henriksen5eca0752008-08-17 18:44:35 +000094 AU.addRequired<GCModuleInfo>();
David Greenefe37ab32009-08-18 19:22:55 +000095 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +000096 AU.addRequired<MachineLoopInfo>();
Gordon Henriksence224772008-01-07 01:30:38 +000097}
98
Chris Lattnera80ba712004-08-16 23:15:22 +000099bool AsmPrinter::doInitialization(Module &M) {
Chris Lattnerf26e03b2009-07-31 17:42:42 +0000100 // Initialize TargetLoweringObjectFile.
101 const_cast<TargetLoweringObjectFile&>(getObjFileLowering())
102 .Initialize(OutContext, TM);
103
Chris Lattner33adcfb2009-08-22 21:43:10 +0000104 Mang = new Mangler(M, MAI->getGlobalPrefix(), MAI->getPrivateGlobalPrefix(),
105 MAI->getLinkerPrivateGlobalPrefix());
Chris Lattner2c1b1592006-01-23 23:47:53 +0000106
Chris Lattner33adcfb2009-08-22 21:43:10 +0000107 if (MAI->doesAllowQuotesInName())
Chris Lattnera93ca922009-06-18 23:41:35 +0000108 Mang->setUseQuotes(true);
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +0000109
110 if (MAI->doesAllowNameToStartWithDigit())
111 Mang->setSymbolsCanStartWithDigit(true);
Chris Lattnera93ca922009-06-18 23:41:35 +0000112
Bob Wilson812209a2009-09-30 22:06:26 +0000113 // Allow the target to emit any magic that it wants at the start of the file.
114 EmitStartOfAsmFile(M);
Rafael Espindola952b8392008-12-03 11:01:37 +0000115
Chris Lattner33adcfb2009-08-22 21:43:10 +0000116 if (MAI->hasSingleParameterDotFile()) {
Rafael Espindola952b8392008-12-03 11:01:37 +0000117 /* Very minimal debug info. It is ignored if we emit actual
Bob Wilsonbc9506f2009-09-30 21:26:13 +0000118 debug info. If we don't, this at least helps the user find where
Rafael Espindola952b8392008-12-03 11:01:37 +0000119 a function came from. */
120 O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
121 }
122
Bob Wilson812209a2009-09-30 22:06:26 +0000123 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
124 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000125 for (GCModuleInfo::iterator I = MI->begin(), E = MI->end(); I != E; ++I)
126 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000127 MP->beginAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000128
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000129 if (!M.getModuleInlineAsm().empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000130 O << MAI->getCommentString() << " Start of file scope inline assembly\n"
Chris Lattner3e2fa7a2006-01-24 04:16:34 +0000131 << M.getModuleInlineAsm()
Chris Lattner33adcfb2009-08-22 21:43:10 +0000132 << '\n' << MAI->getCommentString()
Jim Laskey563321a2006-09-06 18:34:40 +0000133 << " End of file scope inline assembly\n";
Chris Lattner2c1b1592006-01-23 23:47:53 +0000134
Chris Lattnerb55e0682009-09-24 05:44:53 +0000135 MMI = getAnalysisIfAvailable<MachineModuleInfo>();
136 if (MMI)
137 MMI->AnalyzeModule(M);
138 DW = getAnalysisIfAvailable<DwarfWriter>();
139 if (DW)
140 DW->BeginModule(&M, MMI, O, this, MAI);
Devang Patel14a55d92009-06-19 21:54:26 +0000141
Chris Lattnera80ba712004-08-16 23:15:22 +0000142 return false;
143}
144
145bool AsmPrinter::doFinalization(Module &M) {
Chris Lattner40bbebd2009-07-21 18:38:57 +0000146 // Emit global variables.
147 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
148 I != E; ++I)
149 PrintGlobalVariable(I);
150
Chris Lattner1f522fe2009-06-24 18:54:37 +0000151 // Emit final debug information.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000152 if (MAI->doesSupportDebugInformation() || MAI->doesSupportExceptionHandling())
Chris Lattner1f522fe2009-06-24 18:54:37 +0000153 DW->EndModule();
154
Chris Lattner0a7befa2009-06-24 18:52:01 +0000155 // If the target wants to know about weak references, print them all.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000156 if (MAI->getWeakRefDirective()) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000157 // FIXME: This is not lazy, it would be nice to only print weak references
158 // to stuff that is actually used. Note that doing so would require targets
159 // to notice uses in operands (due to constant exprs etc). This should
160 // happen with the MC stuff eventually.
Rafael Espindola15404d02006-12-18 03:37:18 +0000161
Chris Lattner0a7befa2009-06-24 18:52:01 +0000162 // Print out module-level global variables here.
163 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
164 I != E; ++I) {
165 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000166 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000167 }
168
Chris Lattnerc6fdced2009-08-03 23:10:34 +0000169 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner0a7befa2009-06-24 18:52:01 +0000170 if (I->hasExternalWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000171 O << MAI->getWeakRefDirective() << Mang->getMangledName(I) << '\n';
Chris Lattner0a7befa2009-06-24 18:52:01 +0000172 }
Rafael Espindola15404d02006-12-18 03:37:18 +0000173 }
174
Chris Lattner33adcfb2009-08-22 21:43:10 +0000175 if (MAI->getSetDirective()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000176 O << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000177 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner0a7befa2009-06-24 18:52:01 +0000178 I != E; ++I) {
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000179 std::string Name = Mang->getMangledName(I);
Anton Korobeynikov325be7c2007-09-06 17:21:48 +0000180
181 const GlobalValue *GV = cast<GlobalValue>(I->getAliasedGlobal());
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000182 std::string Target = Mang->getMangledName(GV);
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000183
Chris Lattner33adcfb2009-08-22 21:43:10 +0000184 if (I->hasExternalLinkage() || !MAI->getWeakRefDirective())
Dan Gohmand19a53b2008-06-30 22:03:41 +0000185 O << "\t.globl\t" << Name << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000186 else if (I->hasWeakLinkage())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000187 O << MAI->getWeakRefDirective() << Name << '\n';
Rafael Espindolabb46f522009-01-15 20:18:42 +0000188 else if (!I->hasLocalLinkage())
Torok Edwinc23197a2009-07-14 16:55:14 +0000189 llvm_unreachable("Invalid alias linkage");
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000190
Anton Korobeynikov541af7f2008-09-24 22:21:04 +0000191 printVisibility(Name, I->getVisibility());
Anton Korobeynikov22c9e652008-03-11 21:41:14 +0000192
Chris Lattner33adcfb2009-08-22 21:43:10 +0000193 O << MAI->getSetDirective() << ' ' << Name << ", " << Target << '\n';
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000194 }
195 }
196
Duncan Sands1465d612009-01-28 13:14:17 +0000197 GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000198 assert(MI && "AsmPrinter didn't require GCModuleInfo?");
199 for (GCModuleInfo::iterator I = MI->end(), E = MI->begin(); I != E; )
200 if (GCMetadataPrinter *MP = GetOrCreateGCPrinter(*--I))
Chris Lattner33adcfb2009-08-22 21:43:10 +0000201 MP->finishAssembly(O, *this, *MAI);
Gordon Henriksence224772008-01-07 01:30:38 +0000202
Dan Gohmana779a982008-05-05 00:28:39 +0000203 // If we don't have any trampolines, then we don't require stack memory
204 // to be executable. Some targets have a directive to declare this.
Chris Lattner0a7befa2009-06-24 18:52:01 +0000205 Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
Dan Gohmana779a982008-05-05 00:28:39 +0000206 if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000207 if (MAI->getNonexecutableStackDirective())
208 O << MAI->getNonexecutableStackDirective() << '\n';
Dan Gohmana779a982008-05-05 00:28:39 +0000209
Chris Lattnerbd23d5f2009-09-18 20:17:03 +0000210
211 // Allow the target to emit any magic that it wants at the end of the file,
212 // after everything else has gone out.
213 EmitEndOfAsmFile(M);
214
Chris Lattnera80ba712004-08-16 23:15:22 +0000215 delete Mang; Mang = 0;
Chris Lattner0de1fc42009-06-24 19:09:55 +0000216 DW = 0; MMI = 0;
Chris Lattner2b2954f2009-07-27 21:28:04 +0000217
218 OutStreamer.Finish();
Chris Lattnera80ba712004-08-16 23:15:22 +0000219 return false;
220}
221
Chris Lattner25045bd2005-11-21 07:51:36 +0000222void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
Chris Lattnera80ba712004-08-16 23:15:22 +0000223 // What's my mangled name?
Chris Lattnerb8158ac2009-07-14 18:17:16 +0000224 CurrentFnName = Mang->getMangledName(MF.getFunction());
Evan Cheng347d39f2007-10-14 05:57:21 +0000225 IncrementFunctionNumber();
David Greeneb71d1b22009-08-10 16:38:07 +0000226
Chris Lattner25d812b2009-09-16 00:35:39 +0000227 if (VerboseAsm)
David Greeneb71d1b22009-08-10 16:38:07 +0000228 LI = &getAnalysis<MachineLoopInfo>();
Chris Lattnera80ba712004-08-16 23:15:22 +0000229}
230
Evan Cheng1606e8e2009-03-13 07:51:59 +0000231namespace {
232 // SectionCPs - Keep track the alignment, constpool entries per Section.
233 struct SectionCPs {
Chris Lattnera87dea42009-07-31 18:48:30 +0000234 const MCSection *S;
Evan Cheng1606e8e2009-03-13 07:51:59 +0000235 unsigned Alignment;
236 SmallVector<unsigned, 4> CPEs;
Chris Lattnera87dea42009-07-31 18:48:30 +0000237 SectionCPs(const MCSection *s, unsigned a) : S(s), Alignment(a) {};
Evan Cheng1606e8e2009-03-13 07:51:59 +0000238 };
239}
240
Chris Lattner3b4fd322005-11-21 08:25:09 +0000241/// EmitConstantPool - Print to the current output stream assembly
242/// representations of the constants in the constant pool MCP. This is
243/// used to print out constants which have been "spilled to memory" by
244/// the code generator.
245///
246void AsmPrinter::EmitConstantPool(MachineConstantPool *MCP) {
Chris Lattnerfa77d432006-02-09 04:22:52 +0000247 const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
Chris Lattner3b4fd322005-11-21 08:25:09 +0000248 if (CP.empty()) return;
Evan Cheng2d2cec12006-06-29 00:26:09 +0000249
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000250 // Calculate sections for constant pool entries. We collect entries to go into
251 // the same section together to reduce amount of section switch statements.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000252 SmallVector<SectionCPs, 4> CPSections;
Chris Lattner3b4fd322005-11-21 08:25:09 +0000253 for (unsigned i = 0, e = CP.size(); i != e; ++i) {
Chris Lattner298414e2009-07-22 00:28:43 +0000254 const MachineConstantPoolEntry &CPE = CP[i];
Evan Cheng1606e8e2009-03-13 07:51:59 +0000255 unsigned Align = CPE.getAlignment();
Chris Lattner5c2f7892009-07-26 06:26:55 +0000256
257 SectionKind Kind;
258 switch (CPE.getRelocationInfo()) {
259 default: llvm_unreachable("Unknown section kind");
Chris Lattner27981192009-08-01 23:57:16 +0000260 case 2: Kind = SectionKind::getReadOnlyWithRel(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000261 case 1:
Chris Lattner27981192009-08-01 23:57:16 +0000262 Kind = SectionKind::getReadOnlyWithRelLocal();
Chris Lattner4c509222009-07-26 07:00:12 +0000263 break;
Chris Lattner5c2f7892009-07-26 06:26:55 +0000264 case 0:
Chris Lattner4c509222009-07-26 07:00:12 +0000265 switch (TM.getTargetData()->getTypeAllocSize(CPE.getType())) {
Chris Lattner27981192009-08-01 23:57:16 +0000266 case 4: Kind = SectionKind::getMergeableConst4(); break;
267 case 8: Kind = SectionKind::getMergeableConst8(); break;
268 case 16: Kind = SectionKind::getMergeableConst16();break;
269 default: Kind = SectionKind::getMergeableConst(); break;
Chris Lattner4c509222009-07-26 07:00:12 +0000270 }
Chris Lattner5c2f7892009-07-26 06:26:55 +0000271 }
272
Chris Lattner83d77fa2009-08-01 23:46:12 +0000273 const MCSection *S = getObjFileLowering().getSectionForConstant(Kind);
Chris Lattner298414e2009-07-22 00:28:43 +0000274
Evan Cheng1606e8e2009-03-13 07:51:59 +0000275 // The number of sections are small, just do a linear search from the
276 // last section to the first.
277 bool Found = false;
278 unsigned SecIdx = CPSections.size();
279 while (SecIdx != 0) {
280 if (CPSections[--SecIdx].S == S) {
281 Found = true;
282 break;
283 }
284 }
285 if (!Found) {
286 SecIdx = CPSections.size();
287 CPSections.push_back(SectionCPs(S, Align));
288 }
289
290 if (Align > CPSections[SecIdx].Alignment)
291 CPSections[SecIdx].Alignment = Align;
292 CPSections[SecIdx].CPEs.push_back(i);
Evan Cheng2d2cec12006-06-29 00:26:09 +0000293 }
294
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000295 // Now print stuff into the calculated sections.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000296 for (unsigned i = 0, e = CPSections.size(); i != e; ++i) {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000297 OutStreamer.SwitchSection(CPSections[i].S);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000298 EmitAlignment(Log2_32(CPSections[i].Alignment));
Evan Cheng2d2cec12006-06-29 00:26:09 +0000299
Evan Cheng1606e8e2009-03-13 07:51:59 +0000300 unsigned Offset = 0;
301 for (unsigned j = 0, ee = CPSections[i].CPEs.size(); j != ee; ++j) {
302 unsigned CPI = CPSections[i].CPEs[j];
303 MachineConstantPoolEntry CPE = CP[CPI];
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000304
Chris Lattner3029f922006-02-09 04:46:04 +0000305 // Emit inter-object padding for alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +0000306 unsigned AlignMask = CPE.getAlignment() - 1;
307 unsigned NewOffset = (Offset + AlignMask) & ~AlignMask;
308 EmitZeros(NewOffset - Offset);
309
310 const Type *Ty = CPE.getType();
Duncan Sands777d2302009-05-09 07:06:46 +0000311 Offset = NewOffset + TM.getTargetData()->getTypeAllocSize(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +0000312
Chris Lattner33adcfb2009-08-22 21:43:10 +0000313 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Dan Gohmana12e9d72009-08-12 18:32:22 +0000314 << CPI << ':';
Evan Cheng1606e8e2009-03-13 07:51:59 +0000315 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000316 O.PadToColumn(MAI->getCommentColumn());
317 O << MAI->getCommentString() << " constant ";
Dan Gohmancf20ac42009-08-13 01:36:44 +0000318 WriteTypeSymbolic(O, CPE.getType(), MF->getFunction()->getParent());
Anton Korobeynikov088ae832008-09-24 22:17:59 +0000319 }
Evan Cheng1606e8e2009-03-13 07:51:59 +0000320 O << '\n';
321 if (CPE.isMachineConstantPoolEntry())
322 EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
323 else
324 EmitGlobalConstant(CPE.Val.ConstVal);
Chris Lattner3029f922006-02-09 04:46:04 +0000325 }
Chris Lattner3b4fd322005-11-21 08:25:09 +0000326 }
327}
328
Nate Begeman37efe672006-04-22 18:53:45 +0000329/// EmitJumpTableInfo - Print assembly representations of the jump tables used
330/// by the current function to the current output stream.
331///
Chris Lattner1da31ee2006-10-05 03:01:21 +0000332void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
333 MachineFunction &MF) {
Nate Begeman37efe672006-04-22 18:53:45 +0000334 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
335 if (JT.empty()) return;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000336
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000337 bool IsPic = TM.getRelocationModel() == Reloc::PIC_;
Nate Begeman4d9bbdc2006-07-27 16:46:58 +0000338
Nate Begeman2f1ae882006-07-27 01:13:04 +0000339 // Pick the directive to use to print the jump table entries, and switch to
340 // the appropriate section.
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000341 TargetLowering *LoweringInfo = TM.getTargetLowering();
Anton Korobeynikov24287dd2006-12-19 21:04:20 +0000342
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000343 const Function *F = MF.getFunction();
Evan Chengb13bafe2009-06-18 20:37:15 +0000344 bool JTInDiffSection = false;
Chris Lattner83d77fa2009-08-01 23:46:12 +0000345 if (F->isWeakForLinker() ||
346 (IsPic && !LoweringInfo->usesGlobalOffsetTable())) {
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000347 // In PIC mode, we need to emit the jump table to the same section as the
348 // function body itself, otherwise the label differences won't make sense.
Anton Korobeynikove87f52d2008-07-09 13:27:16 +0000349 // We should also do if the section name is NULL or function is declared in
350 // discardable section.
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000351 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang,
352 TM));
Nate Begeman2f1ae882006-07-27 01:13:04 +0000353 } else {
Chris Lattner83d77fa2009-08-01 23:46:12 +0000354 // Otherwise, drop it in the readonly section.
355 const MCSection *ReadOnlySection =
Chris Lattner27981192009-08-01 23:57:16 +0000356 getObjFileLowering().getSectionForConstant(SectionKind::getReadOnly());
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000357 OutStreamer.SwitchSection(ReadOnlySection);
Evan Chengb13bafe2009-06-18 20:37:15 +0000358 JTInDiffSection = true;
Nate Begeman2f1ae882006-07-27 01:13:04 +0000359 }
Jim Laskeyacd80ac2006-12-14 19:17:33 +0000360
361 EmitAlignment(Log2_32(MJTI->getAlignment()));
Chris Lattner0c4e6782006-07-15 01:34:12 +0000362
Nate Begeman37efe672006-04-22 18:53:45 +0000363 for (unsigned i = 0, e = JT.size(); i != e; ++i) {
Nate Begeman52a51e382006-08-12 21:29:52 +0000364 const std::vector<MachineBasicBlock*> &JTBBs = JT[i].MBBs;
Chris Lattner07371882006-10-28 18:10:06 +0000365
366 // If this jump table was deleted, ignore it.
367 if (JTBBs.empty()) continue;
Nate Begeman52a51e382006-08-12 21:29:52 +0000368
369 // For PIC codegen, if possible we want to use the SetDirective to reduce
370 // the number of relocations the assembler will generate for the jump table.
371 // Set directives are all printed before the jump table itself.
Evan Chengcc415862007-11-09 01:32:10 +0000372 SmallPtrSet<MachineBasicBlock*, 16> EmittedSets;
Chris Lattner33adcfb2009-08-22 21:43:10 +0000373 if (MAI->getSetDirective() && IsPic)
Nate Begeman52a51e382006-08-12 21:29:52 +0000374 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
Evan Chengcc415862007-11-09 01:32:10 +0000375 if (EmittedSets.insert(JTBBs[ii]))
376 printPICJumpTableSetLabel(i, JTBBs[ii]);
Nate Begeman52a51e382006-08-12 21:29:52 +0000377
Chris Lattner7c301912009-09-13 19:02:16 +0000378 // On some targets (e.g. Darwin) we want to emit two consequtive labels
Chris Lattner393a8ee2007-01-18 01:12:56 +0000379 // before each jump table. The first label is never referenced, but tells
380 // the assembler and linker the extents of the jump table object. The
381 // second label is actually referenced by the code.
Chris Lattner7c301912009-09-13 19:02:16 +0000382 if (JTInDiffSection && MAI->getLinkerPrivateGlobalPrefix()[0]) {
383 O << MAI->getLinkerPrivateGlobalPrefix()
384 << "JTI" << getFunctionNumber() << '_' << i << ":\n";
Evan Chengb13bafe2009-06-18 20:37:15 +0000385 }
Chris Lattner393a8ee2007-01-18 01:12:56 +0000386
Chris Lattner33adcfb2009-08-22 21:43:10 +0000387 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +0000388 << '_' << i << ":\n";
Nate Begeman52a51e382006-08-12 21:29:52 +0000389
Nate Begeman37efe672006-04-22 18:53:45 +0000390 for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000391 printPICJumpTableEntry(MJTI, JTBBs[ii], i);
Nate Begeman37efe672006-04-22 18:53:45 +0000392 O << '\n';
393 }
394 }
395}
396
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000397void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
398 const MachineBasicBlock *MBB,
399 unsigned uid) const {
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000400 bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000401
402 // Use JumpTableDirective otherwise honor the entry size from the jump table
403 // info.
Chris Lattner33adcfb2009-08-22 21:43:10 +0000404 const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000405 bool HadJTEntryDirective = JTEntryDirective != NULL;
406 if (!HadJTEntryDirective) {
407 JTEntryDirective = MJTI->getEntrySize() == 4 ?
Chris Lattner33adcfb2009-08-22 21:43:10 +0000408 MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000409 }
410
411 O << JTEntryDirective << ' ';
412
413 // If we have emitted set directives for the jump table entries, print
414 // them rather than the entries themselves. If we're emitting PIC, then
415 // emit the table entries as differences between two text section labels.
416 // If we're emitting non-PIC code, then emit the entries as direct
417 // references to the target basic blocks.
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000418 if (!isPIC) {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000419 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +0000420 } else if (MAI->getSetDirective()) {
421 O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000422 << '_' << uid << "_set_" << MBB->getNumber();
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000423 } else {
Chris Lattner325d3dc2009-09-13 17:14:04 +0000424 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000425 // If the arch uses custom Jump Table directives, don't calc relative to
426 // JT
427 if (!HadJTEntryDirective)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000428 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI"
Chris Lattnerb3ac6e42009-08-11 20:29:57 +0000429 << getFunctionNumber() << '_' << uid;
Anton Korobeynikov9de19342007-11-14 09:18:41 +0000430 }
431}
432
433
Chris Lattnered138932005-12-13 06:32:10 +0000434/// EmitSpecialLLVMGlobal - Check to see if the specified global is a
435/// special global used by LLVM. If so, emit it and return true, otherwise
436/// do nothing and return false.
437bool AsmPrinter::EmitSpecialLLVMGlobal(const GlobalVariable *GV) {
Daniel Dunbar03d76512009-07-25 23:55:21 +0000438 if (GV->getName() == "llvm.used") {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000439 if (MAI->getUsedDirective() != 0) // No need to emit this at all.
Andrew Lenharthb753a9b2007-08-22 19:33:11 +0000440 EmitLLVMUsedList(GV->getInitializer());
441 return true;
442 }
443
Chris Lattner401e10c2009-07-20 06:14:25 +0000444 // Ignore debug and non-emitted data. This handles llvm.compiler.used.
Chris Lattner266c7bb2009-04-13 05:44:34 +0000445 if (GV->getSection() == "llvm.metadata" ||
446 GV->hasAvailableExternallyLinkage())
447 return true;
Jim Laskey78098112006-03-07 22:00:35 +0000448
449 if (!GV->hasAppendingLinkage()) return false;
450
451 assert(GV->hasInitializer() && "Not a special LLVM global!");
Chris Lattnered138932005-12-13 06:32:10 +0000452
Evan Cheng916d07c2007-06-04 20:39:18 +0000453 const TargetData *TD = TM.getTargetData();
454 unsigned Align = Log2_32(TD->getPointerPrefAlignment());
Chris Lattnerf231c072009-03-09 05:52:15 +0000455 if (GV->getName() == "llvm.global_ctors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000456 OutStreamer.SwitchSection(getObjFileLowering().getStaticCtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000457 EmitAlignment(Align, 0);
458 EmitXXStructorList(GV->getInitializer());
459 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000460 }
461
Chris Lattnerf231c072009-03-09 05:52:15 +0000462 if (GV->getName() == "llvm.global_dtors") {
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000463 OutStreamer.SwitchSection(getObjFileLowering().getStaticDtorSection());
Chris Lattnerea3a9ff2009-03-09 08:18:48 +0000464 EmitAlignment(Align, 0);
465 EmitXXStructorList(GV->getInitializer());
466 return true;
Chris Lattnered138932005-12-13 06:32:10 +0000467 }
468
469 return false;
470}
471
Chris Lattner33adcfb2009-08-22 21:43:10 +0000472/// EmitLLVMUsedList - For targets that define a MAI::UsedDirective, mark each
Dale Johannesend2e51af2008-09-09 22:29:13 +0000473/// global in the specified llvm.used list for which emitUsedDirectiveFor
474/// is true, as being used with this directive.
Chris Lattnercb05af82006-09-26 03:38:18 +0000475void AsmPrinter::EmitLLVMUsedList(Constant *List) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000476 const char *Directive = MAI->getUsedDirective();
Chris Lattnercb05af82006-09-26 03:38:18 +0000477
Dan Gohmana119de82009-06-14 23:30:43 +0000478 // Should be an array of 'i8*'.
Chris Lattnercb05af82006-09-26 03:38:18 +0000479 ConstantArray *InitList = dyn_cast<ConstantArray>(List);
480 if (InitList == 0) return;
481
482 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
Chris Lattner16fe9902009-07-17 22:00:23 +0000483 const GlobalValue *GV =
484 dyn_cast<GlobalValue>(InitList->getOperand(i)->stripPointerCasts());
Chris Lattner26630c12009-07-31 20:52:39 +0000485 if (GV && getObjFileLowering().shouldEmitUsedDirectiveFor(GV, Mang)) {
Dale Johannesen61e60932008-09-03 20:34:58 +0000486 O << Directive;
487 EmitConstantValueOnly(InitList->getOperand(i));
488 O << '\n';
489 }
Chris Lattnercb05af82006-09-26 03:38:18 +0000490 }
491}
492
Chris Lattnered138932005-12-13 06:32:10 +0000493/// EmitXXStructorList - Emit the ctor or dtor list. This just prints out the
494/// function pointers, ignoring the init priority.
495void AsmPrinter::EmitXXStructorList(Constant *List) {
496 // Should be an array of '{ int, void ()* }' structs. The first value is the
497 // init priority, which we ignore.
498 if (!isa<ConstantArray>(List)) return;
499 ConstantArray *InitList = cast<ConstantArray>(List);
500 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i)
501 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
502 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
Chris Lattner8de324b2005-12-21 01:17:37 +0000503
504 if (CS->getOperand(1)->isNullValue())
505 return; // Found a null terminator, exit printing.
506 // Emit the function pointer.
Chris Lattnered138932005-12-13 06:32:10 +0000507 EmitGlobalConstant(CS->getOperand(1));
508 }
509}
Chris Lattner3b4fd322005-11-21 08:25:09 +0000510
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000511
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000512//===----------------------------------------------------------------------===//
513/// LEB 128 number encoding.
514
515/// PrintULEB128 - Print a series of hexidecimal values (separated by commas)
516/// representing an unsigned leb128 value.
517void AsmPrinter::PrintULEB128(unsigned Value) const {
Chris Lattnera64f4632008-11-10 04:35:24 +0000518 char Buffer[20];
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000519 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000520 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000521 Value >>= 7;
522 if (Value) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000523 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000524 if (Value) O << ", ";
525 } while (Value);
526}
527
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000528/// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
529/// representing a signed leb128 value.
530void AsmPrinter::PrintSLEB128(int Value) const {
531 int Sign = Value >> (8 * sizeof(Value) - 1);
532 bool IsMore;
Chris Lattnera64f4632008-11-10 04:35:24 +0000533 char Buffer[20];
Anton Korobeynikovffe31d72008-08-16 12:57:46 +0000534
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000535 do {
Chris Lattnera64f4632008-11-10 04:35:24 +0000536 unsigned char Byte = static_cast<unsigned char>(Value & 0x7f);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000537 Value >>= 7;
538 IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
539 if (IsMore) Byte |= 0x80;
Chris Lattnera64f4632008-11-10 04:35:24 +0000540 O << "0x" << utohex_buffer(Byte, Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000541 if (IsMore) O << ", ";
542 } while (IsMore);
543}
544
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000545//===--------------------------------------------------------------------===//
546// Emission and print routines
547//
548
549/// PrintHex - Print a value as a hexidecimal value.
550///
551void AsmPrinter::PrintHex(int Value) const {
Chris Lattnerc6a13462008-11-10 04:30:26 +0000552 char Buffer[20];
Chris Lattnerc6a13462008-11-10 04:30:26 +0000553 O << "0x" << utohex_buffer(static_cast<unsigned>(Value), Buffer+20);
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000554}
555
556/// EOL - Print a newline character to asm stream. If a comment is present
557/// then it will be printed first. Comments should not contain '\n'.
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000558void AsmPrinter::EOL() const {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000559 O << '\n';
Jim Laskeybda9b0e2007-02-21 22:47:38 +0000560}
Owen Anderson25995092008-07-01 21:16:27 +0000561
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000562void AsmPrinter::EOL(const std::string &Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000563 if (VerboseAsm && !Comment.empty()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000564 O.PadToColumn(MAI->getCommentColumn());
565 O << MAI->getCommentString()
Dan Gohmand19a53b2008-06-30 22:03:41 +0000566 << ' '
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000567 << Comment;
568 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000569 O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000570}
571
Owen Anderson25995092008-07-01 21:16:27 +0000572void AsmPrinter::EOL(const char* Comment) const {
Evan Cheng6547e402008-07-01 23:18:29 +0000573 if (VerboseAsm && *Comment) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000574 O.PadToColumn(MAI->getCommentColumn());
575 O << MAI->getCommentString()
Owen Anderson25995092008-07-01 21:16:27 +0000576 << ' '
577 << Comment;
578 }
579 O << '\n';
580}
581
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000582static const char *DecodeDWARFEncoding(unsigned Encoding) {
583 switch (Encoding) {
584 case dwarf::DW_EH_PE_absptr:
585 return "absptr";
586 case dwarf::DW_EH_PE_omit:
587 return "omit";
588 case dwarf::DW_EH_PE_pcrel:
589 return "pcrel";
Bill Wendling0734d352009-09-09 21:26:19 +0000590 case dwarf::DW_EH_PE_udata4:
591 return "udata4";
592 case dwarf::DW_EH_PE_udata8:
593 return "udata8";
594 case dwarf::DW_EH_PE_sdata4:
595 return "sdata4";
596 case dwarf::DW_EH_PE_sdata8:
597 return "sdata8";
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000598 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata4:
599 return "pcrel udata4";
600 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata4:
601 return "pcrel sdata4";
602 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_udata8:
603 return "pcrel udata8";
604 case dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_sdata8:
605 return "pcrel sdata8";
606 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata4:
607 return "indirect pcrel udata4";
608 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata4:
609 return "indirect pcrel sdata4";
610 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_udata8:
611 return "indirect pcrel udata8";
612 case dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_pcrel |dwarf::DW_EH_PE_sdata8:
613 return "indirect pcrel sdata8";
614 }
615
616 return 0;
617}
618
Bill Wendlingb5abecd2009-08-29 12:17:53 +0000619void AsmPrinter::EOL(const char *Comment, unsigned Encoding) const {
620 if (VerboseAsm && *Comment) {
621 O.PadToColumn(MAI->getCommentColumn());
622 O << MAI->getCommentString()
623 << ' '
624 << Comment;
625
626 if (const char *EncStr = DecodeDWARFEncoding(Encoding))
627 O << " (" << EncStr << ')';
628 }
629 O << '\n';
630}
631
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000632/// EmitULEB128Bytes - Emit an assembler byte data directive to compose an
633/// unsigned leb128 value.
634void AsmPrinter::EmitULEB128Bytes(unsigned Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000635 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000636 O << "\t.uleb128\t"
637 << Value;
638 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000639 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000640 PrintULEB128(Value);
641 }
642}
643
644/// EmitSLEB128Bytes - print an assembler byte data directive to compose a
645/// signed leb128 value.
646void AsmPrinter::EmitSLEB128Bytes(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000647 if (MAI->hasLEB128()) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000648 O << "\t.sleb128\t"
649 << Value;
650 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000651 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000652 PrintSLEB128(Value);
653 }
654}
655
656/// EmitInt8 - Emit a byte directive and value.
657///
658void AsmPrinter::EmitInt8(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000659 O << MAI->getData8bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000660 PrintHex(Value & 0xFF);
661}
662
663/// EmitInt16 - Emit a short directive and value.
664///
665void AsmPrinter::EmitInt16(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000666 O << MAI->getData16bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000667 PrintHex(Value & 0xFFFF);
668}
669
670/// EmitInt32 - Emit a long directive and value.
671///
672void AsmPrinter::EmitInt32(int Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000673 O << MAI->getData32bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000674 PrintHex(Value);
675}
676
677/// EmitInt64 - Emit a long long directive and value.
678///
679void AsmPrinter::EmitInt64(uint64_t Value) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000680 if (MAI->getData64bitsDirective()) {
681 O << MAI->getData64bitsDirective();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000682 PrintHex(Value);
683 } else {
684 if (TM.getTargetData()->isBigEndian()) {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000685 EmitInt32(unsigned(Value >> 32)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000686 EmitInt32(unsigned(Value));
687 } else {
Dan Gohmand19a53b2008-06-30 22:03:41 +0000688 EmitInt32(unsigned(Value)); O << '\n';
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000689 EmitInt32(unsigned(Value >> 32));
690 }
691 }
692}
693
694/// toOctal - Convert the low order bits of X into an octal digit.
695///
696static inline char toOctal(int X) {
697 return (X&7)+'0';
698}
699
700/// printStringChar - Print a char, escaped if necessary.
701///
David Greene71847812009-07-14 20:18:05 +0000702static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000703 if (C == '"') {
704 O << "\\\"";
705 } else if (C == '\\') {
706 O << "\\\\";
Chris Lattnerbbfa2442009-01-22 23:38:45 +0000707 } else if (isprint((unsigned char)C)) {
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000708 O << C;
709 } else {
710 switch(C) {
711 case '\b': O << "\\b"; break;
712 case '\f': O << "\\f"; break;
713 case '\n': O << "\\n"; break;
714 case '\r': O << "\\r"; break;
715 case '\t': O << "\\t"; break;
716 default:
717 O << '\\';
718 O << toOctal(C >> 6);
719 O << toOctal(C >> 3);
720 O << toOctal(C >> 0);
721 break;
722 }
723 }
724}
725
726/// EmitString - Emit a string with quotes and a null terminator.
727/// Special characters are emitted properly.
728/// \literal (Eg. '\t') \endliteral
729void AsmPrinter::EmitString(const std::string &String) const {
Bill Wendlingf34be822009-04-09 23:51:31 +0000730 EmitString(String.c_str(), String.size());
731}
732
733void AsmPrinter::EmitString(const char *String, unsigned Size) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000734 const char* AscizDirective = MAI->getAscizDirective();
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000735 if (AscizDirective)
736 O << AscizDirective;
737 else
Chris Lattner33adcfb2009-08-22 21:43:10 +0000738 O << MAI->getAsciiDirective();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000739 O << '\"';
Bill Wendlingf34be822009-04-09 23:51:31 +0000740 for (unsigned i = 0; i < Size; ++i)
Chris Lattnera118c2e2009-04-08 00:28:38 +0000741 printStringChar(O, String[i]);
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000742 if (AscizDirective)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000743 O << '\"';
Anton Korobeynikovfb269cf2007-03-06 19:25:02 +0000744 else
745 O << "\\0\"";
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000746}
747
748
Dan Gohman189f80d2007-09-24 20:58:13 +0000749/// EmitFile - Emit a .file directive.
750void AsmPrinter::EmitFile(unsigned Number, const std::string &Name) const {
751 O << "\t.file\t" << Number << " \"";
Chris Lattnera118c2e2009-04-08 00:28:38 +0000752 for (unsigned i = 0, N = Name.size(); i < N; ++i)
753 printStringChar(O, Name[i]);
Dan Gohmand19a53b2008-06-30 22:03:41 +0000754 O << '\"';
Dan Gohman189f80d2007-09-24 20:58:13 +0000755}
756
757
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000758//===----------------------------------------------------------------------===//
759
Chris Lattner3a420532007-05-31 18:57:45 +0000760// EmitAlignment - Emit an alignment directive to the specified power of
761// two boundary. For example, if you pass in 3 here, you will get an 8
762// byte alignment. If a global value is specified, and if that global has
763// an explicit alignment requested, it will unconditionally override the
764// alignment request. However, if ForcedAlignBits is specified, this value
765// has final say: the ultimate alignment will be the max of ForcedAlignBits
766// and the alignment computed with NumBits and the global.
767//
768// The algorithm is:
769// Align = NumBits;
770// if (GV && GV->hasalignment) Align = GV->getalignment();
771// Align = std::max(Align, ForcedAlignBits);
772//
773void AsmPrinter::EmitAlignment(unsigned NumBits, const GlobalValue *GV,
Evan Cheng05548eb2008-02-29 19:36:59 +0000774 unsigned ForcedAlignBits,
775 bool UseFillExpr) const {
Dale Johannesen00d56b92007-04-23 23:33:31 +0000776 if (GV && GV->getAlignment())
Chris Lattner3a420532007-05-31 18:57:45 +0000777 NumBits = Log2_32(GV->getAlignment());
778 NumBits = std::max(NumBits, ForcedAlignBits);
779
Chris Lattner2a21c6e2005-11-10 18:09:27 +0000780 if (NumBits == 0) return; // No need to emit alignment.
Chris Lattner663c2d22009-08-19 06:12:02 +0000781
782 unsigned FillValue = 0;
Chris Lattnerdabf07c2009-08-18 06:15:16 +0000783 if (getCurrentSection()->getKind().isText())
Chris Lattner33adcfb2009-08-22 21:43:10 +0000784 FillValue = MAI->getTextAlignFillValue();
Chris Lattner663c2d22009-08-19 06:12:02 +0000785
786 OutStreamer.EmitValueToAlignment(1 << NumBits, FillValue, 1, 0);
Chris Lattnerbfddc202004-08-17 19:14:29 +0000787}
David Greenea5bb59f2009-08-05 21:00:52 +0000788
Chris Lattner25045bd2005-11-21 07:51:36 +0000789/// EmitZeros - Emit a block of zeros.
Chris Lattner7d057a32004-08-17 21:38:40 +0000790///
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000791void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
Chris Lattner7d057a32004-08-17 21:38:40 +0000792 if (NumZeros) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000793 if (MAI->getZeroDirective()) {
794 O << MAI->getZeroDirective() << NumZeros;
795 if (MAI->getZeroDirectiveSuffix())
796 O << MAI->getZeroDirectiveSuffix();
Dan Gohmand19a53b2008-06-30 22:03:41 +0000797 O << '\n';
Jeff Cohenc6a057b2006-05-02 03:46:13 +0000798 } else {
Chris Lattner7d057a32004-08-17 21:38:40 +0000799 for (; NumZeros; --NumZeros)
Chris Lattner33adcfb2009-08-22 21:43:10 +0000800 O << MAI->getData8bitsDirective(AddrSpace) << "0\n";
Chris Lattner7d057a32004-08-17 21:38:40 +0000801 }
802 }
803}
804
Chris Lattnera80ba712004-08-16 23:15:22 +0000805// Print out the specified constant, without a storage class. Only the
806// constants valid in constant expressions can occur here.
Chris Lattner25045bd2005-11-21 07:51:36 +0000807void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
Chris Lattnerbd1d3822004-10-16 18:19:26 +0000808 if (CV->isNullValue() || isa<UndefValue>(CV))
Dan Gohmand19a53b2008-06-30 22:03:41 +0000809 O << '0';
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +0000810 else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
Scott Michel4315eee2008-06-03 06:18:19 +0000811 O << CI->getZExtValue();
Reid Spencerb83eb642006-10-20 07:07:24 +0000812 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(CV)) {
Duraid Madina855a5192005-04-02 12:21:51 +0000813 // This is a constant address for a global variable or function. Use the
Chris Lattnerbfd1e502009-09-15 23:11:32 +0000814 // name of the variable or function as the address value.
815 O << Mang->getMangledName(GV);
Duraid Madina855a5192005-04-02 12:21:51 +0000816 } else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
Owen Andersona69571c2006-05-03 01:29:57 +0000817 const TargetData *TD = TM.getTargetData();
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000818 unsigned Opcode = CE->getOpcode();
819 switch (Opcode) {
Chris Lattnerc19ee612009-08-01 22:25:12 +0000820 case Instruction::Trunc:
821 case Instruction::ZExt:
822 case Instruction::SExt:
823 case Instruction::FPTrunc:
824 case Instruction::FPExt:
825 case Instruction::UIToFP:
826 case Instruction::SIToFP:
827 case Instruction::FPToUI:
828 case Instruction::FPToSI:
829 llvm_unreachable("FIXME: Don't support this constant cast expr");
Chris Lattnera80ba712004-08-16 23:15:22 +0000830 case Instruction::GetElementPtr: {
831 // generate a symbolic expression for the byte address
832 const Constant *ptrVal = CE->getOperand(0);
Chris Lattner7f6b9d22007-02-10 20:31:59 +0000833 SmallVector<Value*, 8> idxVec(CE->op_begin()+1, CE->op_end());
834 if (int64_t Offset = TD->getIndexedOffset(ptrVal->getType(), &idxVec[0],
835 idxVec.size())) {
Chris Lattner4798bbe2009-02-05 06:55:21 +0000836 // Truncate/sext the offset to the pointer size.
837 if (TD->getPointerSizeInBits() != 64) {
838 int SExtAmount = 64-TD->getPointerSizeInBits();
839 Offset = (Offset << SExtAmount) >> SExtAmount;
840 }
841
Chris Lattner27e19212005-02-14 21:40:26 +0000842 if (Offset)
Dan Gohmand19a53b2008-06-30 22:03:41 +0000843 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000844 EmitConstantValueOnly(ptrVal);
Chris Lattner27e19212005-02-14 21:40:26 +0000845 if (Offset > 0)
846 O << ") + " << Offset;
847 else if (Offset < 0)
848 O << ") - " << -Offset;
Chris Lattnera80ba712004-08-16 23:15:22 +0000849 } else {
Chris Lattner25045bd2005-11-21 07:51:36 +0000850 EmitConstantValueOnly(ptrVal);
Chris Lattnera80ba712004-08-16 23:15:22 +0000851 }
852 break;
853 }
Chris Lattnercb0a6812006-12-12 05:14:13 +0000854 case Instruction::BitCast:
855 return EmitConstantValueOnly(CE->getOperand(0));
856
Chris Lattnerddc94012006-12-12 05:18:19 +0000857 case Instruction::IntToPtr: {
858 // Handle casts to pointers by changing them into casts to the appropriate
859 // integer type. This promotes constant folding and simplifies this code.
860 Constant *Op = CE->getOperand(0);
Owen Anderson1d0be152009-08-13 21:58:54 +0000861 Op = ConstantExpr::getIntegerCast(Op, TD->getIntPtrType(CV->getContext()),
862 false/*ZExt*/);
Chris Lattnerddc94012006-12-12 05:18:19 +0000863 return EmitConstantValueOnly(Op);
864 }
865
866
867 case Instruction::PtrToInt: {
Chris Lattner4a6bd332006-07-29 01:57:19 +0000868 // Support only foldable casts to/from pointers that can be eliminated by
869 // changing the pointer to the appropriately sized integer type.
Chris Lattnera80ba712004-08-16 23:15:22 +0000870 Constant *Op = CE->getOperand(0);
Chris Lattnerddc94012006-12-12 05:18:19 +0000871 const Type *Ty = CE->getType();
Chris Lattnera80ba712004-08-16 23:15:22 +0000872
Chris Lattnerddc94012006-12-12 05:18:19 +0000873 // We can emit the pointer value into this slot if the slot is an
874 // integer slot greater or equal to the size of the pointer.
Chris Lattnerc19ee612009-08-01 22:25:12 +0000875 if (TD->getTypeAllocSize(Ty) == TD->getTypeAllocSize(Op->getType()))
Chris Lattner4a6bd332006-07-29 01:57:19 +0000876 return EmitConstantValueOnly(Op);
Nick Lewyckyd6227382008-08-08 06:34:07 +0000877
878 O << "((";
Chris Lattner25045bd2005-11-21 07:51:36 +0000879 EmitConstantValueOnly(Op);
Chris Lattnerc19ee612009-08-01 22:25:12 +0000880 APInt ptrMask =
881 APInt::getAllOnesValue(TD->getTypeAllocSizeInBits(Op->getType()));
Chris Lattnerfad86b02008-08-17 07:19:36 +0000882
883 SmallString<40> S;
884 ptrMask.toStringUnsigned(S);
Daniel Dunbardddfd342009-08-19 20:07:03 +0000885 O << ") & " << S.str() << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000886 break;
887 }
888 case Instruction::Add:
Anton Korobeynikov13b7d3d2007-02-04 23:27:42 +0000889 case Instruction::Sub:
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000890 case Instruction::And:
891 case Instruction::Or:
892 case Instruction::Xor:
Dan Gohmand19a53b2008-06-30 22:03:41 +0000893 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000894 EmitConstantValueOnly(CE->getOperand(0));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000895 O << ')';
Anton Korobeynikovfeb88932007-12-18 20:53:41 +0000896 switch (Opcode) {
897 case Instruction::Add:
898 O << " + ";
899 break;
900 case Instruction::Sub:
901 O << " - ";
902 break;
903 case Instruction::And:
904 O << " & ";
905 break;
906 case Instruction::Or:
907 O << " | ";
908 break;
909 case Instruction::Xor:
910 O << " ^ ";
911 break;
912 default:
913 break;
914 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000915 O << '(';
Chris Lattner25045bd2005-11-21 07:51:36 +0000916 EmitConstantValueOnly(CE->getOperand(1));
Dan Gohmand19a53b2008-06-30 22:03:41 +0000917 O << ')';
Chris Lattnera80ba712004-08-16 23:15:22 +0000918 break;
919 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000920 llvm_unreachable("Unsupported operator!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000921 }
Dan Gohman6a55e622009-10-30 01:45:18 +0000922 } else if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
923 GetBlockAddressSymbol(BA)->print(O, MAI);
Chris Lattnera80ba712004-08-16 23:15:22 +0000924 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000925 llvm_unreachable("Unknown constant value!");
Chris Lattnera80ba712004-08-16 23:15:22 +0000926 }
927}
Chris Lattner1b7e2352004-08-17 06:36:49 +0000928
Chris Lattner2980cef2005-11-10 18:06:33 +0000929/// printAsCString - Print the specified array as a C compatible string, only if
Chris Lattner1b7e2352004-08-17 06:36:49 +0000930/// the predicate isString is true.
931///
David Greene71847812009-07-14 20:18:05 +0000932static void printAsCString(formatted_raw_ostream &O, const ConstantArray *CVA,
Chris Lattner2980cef2005-11-10 18:06:33 +0000933 unsigned LastElt) {
Chris Lattner1b7e2352004-08-17 06:36:49 +0000934 assert(CVA->isString() && "Array is not string compatible!");
935
Dan Gohmand19a53b2008-06-30 22:03:41 +0000936 O << '\"';
Chris Lattner2980cef2005-11-10 18:06:33 +0000937 for (unsigned i = 0; i != LastElt; ++i) {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000938 unsigned char C =
Reid Spencerb83eb642006-10-20 07:07:24 +0000939 (unsigned char)cast<ConstantInt>(CVA->getOperand(i))->getZExtValue();
Jim Laskeyf1cdea12007-01-25 15:12:02 +0000940 printStringChar(O, C);
Chris Lattner1b7e2352004-08-17 06:36:49 +0000941 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000942 O << '\"';
Chris Lattner1b7e2352004-08-17 06:36:49 +0000943}
944
Jeff Cohenc884db42006-05-02 01:16:28 +0000945/// EmitString - Emit a zero-byte-terminated string constant.
946///
947void AsmPrinter::EmitString(const ConstantArray *CVA) const {
948 unsigned NumElts = CVA->getNumOperands();
Chris Lattner33adcfb2009-08-22 21:43:10 +0000949 if (MAI->getAscizDirective() && NumElts &&
Reid Spencerb83eb642006-10-20 07:07:24 +0000950 cast<ConstantInt>(CVA->getOperand(NumElts-1))->getZExtValue() == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000951 O << MAI->getAscizDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000952 printAsCString(O, CVA, NumElts-1);
953 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000954 O << MAI->getAsciiDirective();
Jeff Cohenc884db42006-05-02 01:16:28 +0000955 printAsCString(O, CVA, NumElts);
956 }
Dan Gohmand19a53b2008-06-30 22:03:41 +0000957 O << '\n';
Jeff Cohenc884db42006-05-02 01:16:28 +0000958}
959
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000960void AsmPrinter::EmitGlobalConstantArray(const ConstantArray *CVA,
961 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000962 if (CVA->isString()) {
963 EmitString(CVA);
964 } else { // Not a string. Print the values in successive locations
965 for (unsigned i = 0, e = CVA->getNumOperands(); i != e; ++i)
Sanjiv Guptad3d96572009-04-28 16:34:20 +0000966 EmitGlobalConstant(CVA->getOperand(i), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000967 }
968}
969
970void AsmPrinter::EmitGlobalConstantVector(const ConstantVector *CP) {
971 const VectorType *PTy = CP->getType();
972
973 for (unsigned I = 0, E = PTy->getNumElements(); I < E; ++I)
974 EmitGlobalConstant(CP->getOperand(I));
975}
976
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000977void AsmPrinter::EmitGlobalConstantStruct(const ConstantStruct *CVS,
978 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +0000979 // Print the fields in successive locations. Pad to align if needed!
980 const TargetData *TD = TM.getTargetData();
Duncan Sands777d2302009-05-09 07:06:46 +0000981 unsigned Size = TD->getTypeAllocSize(CVS->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000982 const StructLayout *cvsLayout = TD->getStructLayout(CVS->getType());
983 uint64_t sizeSoFar = 0;
984 for (unsigned i = 0, e = CVS->getNumOperands(); i != e; ++i) {
985 const Constant* field = CVS->getOperand(i);
986
987 // Check if padding is needed and insert one or more 0s.
Duncan Sands777d2302009-05-09 07:06:46 +0000988 uint64_t fieldSize = TD->getTypeAllocSize(field->getType());
Dan Gohman00d448a2008-12-22 21:14:27 +0000989 uint64_t padSize = ((i == e-1 ? Size : cvsLayout->getElementOffset(i+1))
990 - cvsLayout->getElementOffset(i)) - fieldSize;
991 sizeSoFar += fieldSize + padSize;
992
993 // Now print the actual field value.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000994 EmitGlobalConstant(field, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +0000995
996 // Insert padding - this may include padding to increase the size of the
997 // current field up to the ABI size (if the struct is not packed) as well
998 // as padding to ensure that the next field starts at the right offset.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +0000999 EmitZeros(padSize, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001000 }
1001 assert(sizeSoFar == cvsLayout->getSizeInBytes() &&
1002 "Layout of constant struct may be incorrect!");
1003}
1004
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001005void AsmPrinter::EmitGlobalConstantFP(const ConstantFP *CFP,
1006 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001007 // FP Constants are printed as integer constants to avoid losing
1008 // precision...
Owen Anderson1d0be152009-08-13 21:58:54 +00001009 LLVMContext &Context = CFP->getContext();
Dan Gohman00d448a2008-12-22 21:14:27 +00001010 const TargetData *TD = TM.getTargetData();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001011 if (CFP->getType()->isDoubleTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001012 double Val = CFP->getValueAPF().convertToDouble(); // for comment only
1013 uint64_t i = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001014 if (MAI->getData64bitsDirective(AddrSpace)) {
1015 O << MAI->getData64bitsDirective(AddrSpace) << i;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001016 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001017 O.PadToColumn(MAI->getCommentColumn());
1018 O << MAI->getCommentString() << " double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001019 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001020 O << '\n';
1021 } else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001022 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001023 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001024 O.PadToColumn(MAI->getCommentColumn());
1025 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001026 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001027 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001028 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001029 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001030 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001031 O.PadToColumn(MAI->getCommentColumn());
1032 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001033 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001034 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001035 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001036 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001037 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001038 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001039 O.PadToColumn(MAI->getCommentColumn());
1040 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001041 << " least significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001042 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001043 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001044 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(i >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001045 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001046 O.PadToColumn(MAI->getCommentColumn());
1047 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001048 << " most significant word of double " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001049 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001050 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001051 }
1052 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001053 }
1054
1055 if (CFP->getType()->isFloatTy()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001056 float Val = CFP->getValueAPF().convertToFloat(); // for comment only
Chris Lattner33adcfb2009-08-22 21:43:10 +00001057 O << MAI->getData32bitsDirective(AddrSpace)
Evan Chengf1c0ae92009-03-24 00:17:40 +00001058 << CFP->getValueAPF().bitcastToAPInt().getZExtValue();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001059 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001060 O.PadToColumn(MAI->getCommentColumn());
1061 O << MAI->getCommentString() << " float " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001062 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001063 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001064 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001065 }
1066
1067 if (CFP->getType()->isX86_FP80Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001068 // all long double variants are printed as hex
1069 // api needed to prevent premature destruction
1070 APInt api = CFP->getValueAPF().bitcastToAPInt();
1071 const uint64_t *p = api.getRawData();
1072 // Convert to double so we can print the approximate val as a comment.
1073 APFloat DoubleVal = CFP->getValueAPF();
1074 bool ignored;
1075 DoubleVal.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
1076 &ignored);
1077 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001078 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001079 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001080 O.PadToColumn(MAI->getCommentColumn());
1081 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001082 << " most significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001083 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001084 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001085 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001086 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001087 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001088 O.PadToColumn(MAI->getCommentColumn());
1089 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001090 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001091 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001092 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001093 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001094 O.PadToColumn(MAI->getCommentColumn());
1095 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001096 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001097 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001098 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001099 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001100 O.PadToColumn(MAI->getCommentColumn());
1101 O << MAI->getCommentString() << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001102 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001103 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001104 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001105 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001106 O.PadToColumn(MAI->getCommentColumn());
1107 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001108 << " least significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001109 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001110 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001111 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001112 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001113 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001114 O.PadToColumn(MAI->getCommentColumn());
1115 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001116 << " least significant halfword of x86_fp80 ~"
Evan Chengf1c0ae92009-03-24 00:17:40 +00001117 << DoubleVal.convertToDouble();
Dan Gohmana12e9d72009-08-12 18:32:22 +00001118 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001119 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001120 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 16);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001121 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001122 O.PadToColumn(MAI->getCommentColumn());
1123 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001124 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001125 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001126 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001127 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001128 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001129 O.PadToColumn(MAI->getCommentColumn());
1130 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001131 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001132 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001133 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001134 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[0] >> 48);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001135 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001136 O.PadToColumn(MAI->getCommentColumn());
1137 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001138 << " next halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001139 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001140 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001141 O << MAI->getData16bitsDirective(AddrSpace) << uint16_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001142 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001143 O.PadToColumn(MAI->getCommentColumn());
1144 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001145 << " most significant halfword";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001146 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001147 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001148 }
Owen Anderson1d0be152009-08-13 21:58:54 +00001149 EmitZeros(TD->getTypeAllocSize(Type::getX86_FP80Ty(Context)) -
1150 TD->getTypeStoreSize(Type::getX86_FP80Ty(Context)), AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001151 return;
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001152 }
1153
1154 if (CFP->getType()->isPPC_FP128Ty()) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001155 // all long double variants are printed as hex
1156 // api needed to prevent premature destruction
1157 APInt api = CFP->getValueAPF().bitcastToAPInt();
1158 const uint64_t *p = api.getRawData();
1159 if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001160 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001161 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001162 O.PadToColumn(MAI->getCommentColumn());
1163 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001164 << " most significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001165 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001166 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001167 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001168 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001169 O.PadToColumn(MAI->getCommentColumn());
1170 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001171 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001172 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001173 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001174 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001175 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001176 O.PadToColumn(MAI->getCommentColumn());
1177 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001178 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001179 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001180 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001181 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001182 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001183 O.PadToColumn(MAI->getCommentColumn());
1184 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001185 << " least significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001186 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001187 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001188 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001189 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001190 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001191 O.PadToColumn(MAI->getCommentColumn());
1192 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001193 << " least significant word of ppc_fp128";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001194 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001195 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001196 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[1] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001197 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001198 O.PadToColumn(MAI->getCommentColumn());
1199 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001200 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001201 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001202 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001203 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0]);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001204 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001205 O.PadToColumn(MAI->getCommentColumn());
1206 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001207 << " next word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001208 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001209 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001210 O << MAI->getData32bitsDirective(AddrSpace) << uint32_t(p[0] >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001211 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001212 O.PadToColumn(MAI->getCommentColumn());
1213 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001214 << " most significant word";
Dan Gohmana12e9d72009-08-12 18:32:22 +00001215 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001216 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001217 }
1218 return;
Torok Edwinc23197a2009-07-14 16:55:14 +00001219 } else llvm_unreachable("Floating point constant type not handled");
Dan Gohman00d448a2008-12-22 21:14:27 +00001220}
1221
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001222void AsmPrinter::EmitGlobalConstantLargeInt(const ConstantInt *CI,
1223 unsigned AddrSpace) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001224 const TargetData *TD = TM.getTargetData();
1225 unsigned BitWidth = CI->getBitWidth();
1226 assert(isPowerOf2_32(BitWidth) &&
1227 "Non-power-of-2-sized integers not handled!");
1228
1229 // We don't expect assemblers to support integer data directives
1230 // for more than 64 bits, so we emit the data in at most 64-bit
1231 // quantities at a time.
1232 const uint64_t *RawData = CI->getValue().getRawData();
1233 for (unsigned i = 0, e = BitWidth / 64; i != e; ++i) {
1234 uint64_t Val;
1235 if (TD->isBigEndian())
1236 Val = RawData[e - i - 1];
1237 else
1238 Val = RawData[i];
1239
Chris Lattner33adcfb2009-08-22 21:43:10 +00001240 if (MAI->getData64bitsDirective(AddrSpace))
1241 O << MAI->getData64bitsDirective(AddrSpace) << Val << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001242 else if (TD->isBigEndian()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001243 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001244 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001245 O.PadToColumn(MAI->getCommentColumn());
1246 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001247 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001248 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001249 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001250 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001251 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001252 O.PadToColumn(MAI->getCommentColumn());
1253 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001254 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001255 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001256 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001257 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001258 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001259 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001260 O.PadToColumn(MAI->getCommentColumn());
1261 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001262 << " least significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001263 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001264 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001265 O << MAI->getData32bitsDirective(AddrSpace) << unsigned(Val >> 32);
Dan Gohmana12e9d72009-08-12 18:32:22 +00001266 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001267 O.PadToColumn(MAI->getCommentColumn());
1268 O << MAI->getCommentString()
Dan Gohmancf20ac42009-08-13 01:36:44 +00001269 << " most significant half of i64 " << Val;
Dan Gohmana12e9d72009-08-12 18:32:22 +00001270 }
Evan Chengf1c0ae92009-03-24 00:17:40 +00001271 O << '\n';
Dan Gohman00d448a2008-12-22 21:14:27 +00001272 }
1273 }
1274}
1275
Chris Lattner25045bd2005-11-21 07:51:36 +00001276/// EmitGlobalConstant - Print a general LLVM constant to the .s file.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001277void AsmPrinter::EmitGlobalConstant(const Constant *CV, unsigned AddrSpace) {
Owen Andersona69571c2006-05-03 01:29:57 +00001278 const TargetData *TD = TM.getTargetData();
Dan Gohman00d448a2008-12-22 21:14:27 +00001279 const Type *type = CV->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00001280 unsigned Size = TD->getTypeAllocSize(type);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001281
Chris Lattnerbd1d3822004-10-16 18:19:26 +00001282 if (CV->isNullValue() || isa<UndefValue>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001283 EmitZeros(Size, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001284 return;
1285 } else if (const ConstantArray *CVA = dyn_cast<ConstantArray>(CV)) {
Sanjiv Guptad3d96572009-04-28 16:34:20 +00001286 EmitGlobalConstantArray(CVA , AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001287 return;
1288 } else if (const ConstantStruct *CVS = dyn_cast<ConstantStruct>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001289 EmitGlobalConstantStruct(CVS, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001290 return;
1291 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001292 EmitGlobalConstantFP(CFP, AddrSpace);
Dan Gohman00d448a2008-12-22 21:14:27 +00001293 return;
1294 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1295 // Small integers are handled below; large integers are handled here.
1296 if (Size > 4) {
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001297 EmitGlobalConstantLargeInt(CI, AddrSpace);
Chris Lattner1b7e2352004-08-17 06:36:49 +00001298 return;
1299 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001300 } else if (const ConstantVector *CP = dyn_cast<ConstantVector>(CV)) {
Dan Gohman00d448a2008-12-22 21:14:27 +00001301 EmitGlobalConstantVector(CP);
Nate Begeman8cfa57b2005-12-06 06:18:55 +00001302 return;
Chris Lattner1b7e2352004-08-17 06:36:49 +00001303 }
1304
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001305 printDataDirective(type, AddrSpace);
Chris Lattner25045bd2005-11-21 07:51:36 +00001306 EmitConstantValueOnly(CV);
Evan Chengf1c0ae92009-03-24 00:17:40 +00001307 if (VerboseAsm) {
1308 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1309 SmallString<40> S;
1310 CI->getValue().toStringUnsigned(S, 16);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001311 O.PadToColumn(MAI->getCommentColumn());
1312 O << MAI->getCommentString() << " 0x" << S.str();
Evan Chengf1c0ae92009-03-24 00:17:40 +00001313 }
Scott Micheleefc8452008-06-03 15:39:51 +00001314 }
Dan Gohmand19a53b2008-06-30 22:03:41 +00001315 O << '\n';
Chris Lattner1b7e2352004-08-17 06:36:49 +00001316}
Chris Lattner0264d1a2006-01-27 02:10:10 +00001317
Chris Lattnerfad86b02008-08-17 07:19:36 +00001318void AsmPrinter::EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001319 // Target doesn't support this yet!
Torok Edwinc23197a2009-07-14 16:55:14 +00001320 llvm_unreachable("Target does not support EmitMachineConstantPoolValue");
Evan Chengd6594ae2006-09-12 21:00:35 +00001321}
1322
Chris Lattner3ce9b672006-09-26 23:59:50 +00001323/// PrintSpecial - Print information related to the specified machine instr
1324/// that is independent of the operand, and may be independent of the instr
1325/// itself. This can be useful for portably encoding the comment character
1326/// or other bits of target-specific knowledge into the asmstrings. The
1327/// syntax used is ${:comment}. Targets can override this to add support
1328/// for their own strange codes.
Chris Lattner3e0cc262009-03-10 05:37:13 +00001329void AsmPrinter::PrintSpecial(const MachineInstr *MI, const char *Code) const {
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001330 if (!strcmp(Code, "private")) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001331 O << MAI->getPrivateGlobalPrefix();
Chris Lattnerbae02cf2006-09-27 00:06:07 +00001332 } else if (!strcmp(Code, "comment")) {
Evan Chengf1c0ae92009-03-24 00:17:40 +00001333 if (VerboseAsm)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001334 O << MAI->getCommentString();
Chris Lattner3ce9b672006-09-26 23:59:50 +00001335 } else if (!strcmp(Code, "uid")) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001336 // Comparing the address of MI isn't sufficient, because machineinstrs may
1337 // be allocated to the same address across functions.
1338 const Function *ThisF = MI->getParent()->getParent()->getFunction();
1339
Owen Andersonbd58edf2009-06-24 22:28:12 +00001340 // If this is a new LastFn instruction, bump the counter.
1341 if (LastMI != MI || LastFn != ThisF) {
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001342 ++Counter;
1343 LastMI = MI;
Owen Andersonbd58edf2009-06-24 22:28:12 +00001344 LastFn = ThisF;
Chris Lattnerb6a24bf2007-02-05 21:23:52 +00001345 }
Chris Lattner3ce9b672006-09-26 23:59:50 +00001346 O << Counter;
1347 } else {
Torok Edwin7d696d82009-07-11 13:10:19 +00001348 std::string msg;
1349 raw_string_ostream Msg(msg);
1350 Msg << "Unknown special formatter '" << Code
Bill Wendlinge8156192006-12-07 01:30:32 +00001351 << "' for machine instr: " << *MI;
Torok Edwin7d696d82009-07-11 13:10:19 +00001352 llvm_report_error(Msg.str());
Chris Lattner3ce9b672006-09-26 23:59:50 +00001353 }
1354}
1355
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001356/// processDebugLoc - Processes the debug information of each machine
1357/// instruction's DebugLoc.
Devang Patelaf0e2722009-10-06 02:19:11 +00001358void AsmPrinter::processDebugLoc(const MachineInstr *MI,
1359 bool BeforePrintingInsn) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001360 if (!MAI || !DW)
Chris Lattnerd2503292009-08-05 04:09:18 +00001361 return;
Devang Patelb0fdedb2009-09-30 23:12:50 +00001362 DebugLoc DL = MI->getDebugLoc();
Chris Lattner33adcfb2009-08-22 21:43:10 +00001363 if (MAI->doesSupportDebugInformation() && DW->ShouldEmitDwarfDebug()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001364 if (!DL.isUnknown()) {
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001365 DebugLocTuple CurDLT = MF->getDebugLocTuple(DL);
Devang Patelaf0e2722009-10-06 02:19:11 +00001366 if (BeforePrintingInsn) {
Devang Patel1619dc32009-10-13 23:28:53 +00001367 if (CurDLT.Scope != 0 && PrevDLT != CurDLT) {
Devang Patel60b35bd2009-10-06 18:37:31 +00001368 unsigned L = DW->RecordSourceLine(CurDLT.Line, CurDLT.Col,
Devang Patel1619dc32009-10-13 23:28:53 +00001369 CurDLT.Scope);
Devang Patel60b35bd2009-10-06 18:37:31 +00001370 printLabel(L);
Dan Gohmanb953c942009-10-31 20:59:09 +00001371 O << '\n';
Devang Patel60b35bd2009-10-06 18:37:31 +00001372#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1373 DW->SetDbgScopeBeginLabels(MI, L);
1374#endif
1375 } else {
1376#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
1377 DW->SetDbgScopeEndLabels(MI, 0);
1378#endif
Devang Patelaf0e2722009-10-06 02:19:11 +00001379 }
Devang Patel60b35bd2009-10-06 18:37:31 +00001380 }
Argyrios Kyrtzidiscd762402009-05-07 13:55:51 +00001381 PrevDLT = CurDLT;
1382 }
1383 }
1384}
Chris Lattner3ce9b672006-09-26 23:59:50 +00001385
Chris Lattner0264d1a2006-01-27 02:10:10 +00001386/// printInlineAsm - This method formats and prints the specified machine
1387/// instruction that is an inline asm.
1388void AsmPrinter::printInlineAsm(const MachineInstr *MI) const {
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001389 unsigned NumOperands = MI->getNumOperands();
1390
1391 // Count the number of register definitions.
1392 unsigned NumDefs = 0;
Dan Gohmand735b802008-10-03 15:45:36 +00001393 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
Chris Lattner67942f52006-09-05 20:02:51 +00001394 ++NumDefs)
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001395 assert(NumDefs != NumOperands-1 && "No asm string?");
1396
Dan Gohmand735b802008-10-03 15:45:36 +00001397 assert(MI->getOperand(NumDefs).isSymbol() && "No asm string?");
Chris Lattner66099132006-02-01 22:41:11 +00001398
1399 // Disassemble the AsmStr, printing out the literal pieces, the operands, etc.
Chris Lattnerf2b67cf2006-01-30 23:00:08 +00001400 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
Chris Lattner66099132006-02-01 22:41:11 +00001401
Dale Johannesenba2a0b92008-01-29 02:21:21 +00001402 // If this asmstr is empty, just print the #APP/#NOAPP markers.
1403 // These are useful to see where empty asm's wound up.
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001404 if (AsmStr[0] == 0) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001405 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
1406 O << MAI->getCommentString() << MAI->getInlineAsmEnd() << '\n';
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001407 return;
1408 }
1409
Chris Lattner33adcfb2009-08-22 21:43:10 +00001410 O << MAI->getCommentString() << MAI->getInlineAsmStart() << "\n\t";
Chris Lattnerf26f5dd2006-07-28 00:17:20 +00001411
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001412 // The variant of the current asmprinter.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001413 int AsmPrinterVariant = MAI->getAssemblerDialect();
Bill Wendlingeb9a42c2007-01-16 03:42:04 +00001414
Chris Lattner66099132006-02-01 22:41:11 +00001415 int CurVariant = -1; // The number of the {.|.|.} region we are in.
1416 const char *LastEmitted = AsmStr; // One past the last character emitted.
Chris Lattner2cc2f662006-02-01 01:28:23 +00001417
Chris Lattner66099132006-02-01 22:41:11 +00001418 while (*LastEmitted) {
1419 switch (*LastEmitted) {
1420 default: {
1421 // Not a special case, emit the string section literally.
1422 const char *LiteralEnd = LastEmitted+1;
1423 while (*LiteralEnd && *LiteralEnd != '{' && *LiteralEnd != '|' &&
Chris Lattner1c059972006-05-05 21:47:05 +00001424 *LiteralEnd != '}' && *LiteralEnd != '$' && *LiteralEnd != '\n')
Chris Lattner66099132006-02-01 22:41:11 +00001425 ++LiteralEnd;
1426 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1427 O.write(LastEmitted, LiteralEnd-LastEmitted);
1428 LastEmitted = LiteralEnd;
1429 break;
1430 }
Chris Lattner1c059972006-05-05 21:47:05 +00001431 case '\n':
1432 ++LastEmitted; // Consume newline character.
Dan Gohmand19a53b2008-06-30 22:03:41 +00001433 O << '\n'; // Indent code with newline.
Chris Lattner1c059972006-05-05 21:47:05 +00001434 break;
Chris Lattner66099132006-02-01 22:41:11 +00001435 case '$': {
1436 ++LastEmitted; // Consume '$' character.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001437 bool Done = true;
1438
1439 // Handle escapes.
1440 switch (*LastEmitted) {
1441 default: Done = false; break;
1442 case '$': // $$ -> $
Chris Lattner66099132006-02-01 22:41:11 +00001443 if (CurVariant == -1 || CurVariant == AsmPrinterVariant)
1444 O << '$';
1445 ++LastEmitted; // Consume second '$' character.
1446 break;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001447 case '(': // $( -> same as GCC's { character.
1448 ++LastEmitted; // Consume '(' character.
1449 if (CurVariant != -1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001450 llvm_report_error("Nested variants found in inline asm string: '"
1451 + std::string(AsmStr) + "'");
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001452 }
1453 CurVariant = 0; // We're in the first variant now.
1454 break;
1455 case '|':
1456 ++LastEmitted; // consume '|' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001457 if (CurVariant == -1)
1458 O << '|'; // this is gcc's behavior for | outside a variant
1459 else
1460 ++CurVariant; // We're in the next variant.
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001461 break;
1462 case ')': // $) -> same as GCC's } char.
1463 ++LastEmitted; // consume ')' character.
Dale Johannesen8b1e0542008-10-10 21:04:42 +00001464 if (CurVariant == -1)
1465 O << '}'; // this is gcc's behavior for } outside a variant
1466 else
1467 CurVariant = -1;
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001468 break;
Chris Lattner66099132006-02-01 22:41:11 +00001469 }
Chris Lattnerfaf1dae2006-10-03 23:27:09 +00001470 if (Done) break;
Chris Lattner66099132006-02-01 22:41:11 +00001471
1472 bool HasCurlyBraces = false;
1473 if (*LastEmitted == '{') { // ${variable}
1474 ++LastEmitted; // Consume '{' character.
1475 HasCurlyBraces = true;
1476 }
1477
Chris Lattner3e0cc262009-03-10 05:37:13 +00001478 // If we have ${:foo}, then this is not a real operand reference, it is a
1479 // "magic" string reference, just like in .td files. Arrange to call
1480 // PrintSpecial.
1481 if (HasCurlyBraces && *LastEmitted == ':') {
1482 ++LastEmitted;
1483 const char *StrStart = LastEmitted;
1484 const char *StrEnd = strchr(StrStart, '}');
1485 if (StrEnd == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001486 llvm_report_error("Unterminated ${:foo} operand in inline asm string: '"
1487 + std::string(AsmStr) + "'");
Chris Lattner3e0cc262009-03-10 05:37:13 +00001488 }
1489
1490 std::string Val(StrStart, StrEnd);
1491 PrintSpecial(MI, Val.c_str());
1492 LastEmitted = StrEnd+1;
1493 break;
1494 }
1495
Chris Lattner66099132006-02-01 22:41:11 +00001496 const char *IDStart = LastEmitted;
1497 char *IDEnd;
Chris Lattnerfad29122007-01-23 00:36:17 +00001498 errno = 0;
Chris Lattner66099132006-02-01 22:41:11 +00001499 long Val = strtol(IDStart, &IDEnd, 10); // We only accept numbers for IDs.
1500 if (!isdigit(*IDStart) || (Val == 0 && errno == EINVAL)) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001501 llvm_report_error("Bad $ operand number in inline asm string: '"
1502 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001503 }
1504 LastEmitted = IDEnd;
1505
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001506 char Modifier[2] = { 0, 0 };
1507
Chris Lattner66099132006-02-01 22:41:11 +00001508 if (HasCurlyBraces) {
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001509 // If we have curly braces, check for a modifier character. This
1510 // supports syntax like ${0:u}, which correspond to "%u0" in GCC asm.
1511 if (*LastEmitted == ':') {
1512 ++LastEmitted; // Consume ':' character.
1513 if (*LastEmitted == 0) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001514 llvm_report_error("Bad ${:} expression in inline asm string: '"
1515 + std::string(AsmStr) + "'");
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001516 }
1517
1518 Modifier[0] = *LastEmitted;
1519 ++LastEmitted; // Consume modifier character.
1520 }
1521
Chris Lattner66099132006-02-01 22:41:11 +00001522 if (*LastEmitted != '}') {
Torok Edwin7d696d82009-07-11 13:10:19 +00001523 llvm_report_error("Bad ${} expression in inline asm string: '"
1524 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001525 }
1526 ++LastEmitted; // Consume '}' character.
1527 }
1528
1529 if ((unsigned)Val >= NumOperands-1) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001530 llvm_report_error("Invalid $ operand number in inline asm string: '"
1531 + std::string(AsmStr) + "'");
Chris Lattner66099132006-02-01 22:41:11 +00001532 }
1533
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001534 // Okay, we finally have a value number. Ask the target to print this
Chris Lattner66099132006-02-01 22:41:11 +00001535 // operand!
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001536 if (CurVariant == -1 || CurVariant == AsmPrinterVariant) {
1537 unsigned OpNo = 1;
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001538
1539 bool Error = false;
1540
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001541 // Scan to find the machine operand number for the operand.
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001542 for (; Val; --Val) {
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001543 if (OpNo >= MI->getNumOperands()) break;
Chris Lattner9e330492007-12-30 20:50:28 +00001544 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Evan Cheng697cbbf2009-03-20 18:03:34 +00001545 OpNo += InlineAsm::getNumOperandRegisters(OpFlags) + 1;
Chris Lattnerdaf6bc62006-02-24 19:50:58 +00001546 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001547
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001548 if (OpNo >= MI->getNumOperands()) {
1549 Error = true;
Chris Lattnerdd260332006-02-24 20:21:58 +00001550 } else {
Chris Lattner9e330492007-12-30 20:50:28 +00001551 unsigned OpFlags = MI->getOperand(OpNo).getImm();
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001552 ++OpNo; // Skip over the ID number.
1553
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001554 if (Modifier[0]=='l') // labels are target independent
Chris Lattner325d3dc2009-09-13 17:14:04 +00001555 GetMBBSymbol(MI->getOperand(OpNo).getMBB()
1556 ->getNumber())->print(O, MAI);
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001557 else {
1558 AsmPrinter *AP = const_cast<AsmPrinter*>(this);
Dale Johannesen86b49f82008-09-24 01:07:17 +00001559 if ((OpFlags & 7) == 4) {
Dale Johanneseneb57ea72007-11-05 21:20:28 +00001560 Error = AP->PrintAsmMemoryOperand(MI, OpNo, AsmPrinterVariant,
1561 Modifier[0] ? Modifier : 0);
1562 } else {
1563 Error = AP->PrintAsmOperand(MI, OpNo, AsmPrinterVariant,
1564 Modifier[0] ? Modifier : 0);
1565 }
Chris Lattnerfd561cd2006-06-08 18:00:47 +00001566 }
Chris Lattnerdd260332006-02-24 20:21:58 +00001567 }
1568 if (Error) {
Torok Edwin7d696d82009-07-11 13:10:19 +00001569 std::string msg;
1570 raw_string_ostream Msg(msg);
1571 Msg << "Invalid operand found in inline asm: '"
Bill Wendlinge8156192006-12-07 01:30:32 +00001572 << AsmStr << "'\n";
Torok Edwin7d696d82009-07-11 13:10:19 +00001573 MI->print(Msg);
1574 llvm_report_error(Msg.str());
Chris Lattner66099132006-02-01 22:41:11 +00001575 }
Chris Lattnerc3a9f8d2006-02-23 19:21:04 +00001576 }
Chris Lattner66099132006-02-01 22:41:11 +00001577 break;
1578 }
Chris Lattner66099132006-02-01 22:41:11 +00001579 }
1580 }
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001581 O << "\n\t" << MAI->getCommentString() << MAI->getInlineAsmEnd();
Chris Lattner66099132006-02-01 22:41:11 +00001582}
1583
Evan Chengda47e6e2008-03-15 00:03:38 +00001584/// printImplicitDef - This method prints the specified machine instruction
1585/// that is an implicit def.
1586void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001587 if (!VerboseAsm) return;
1588 O.PadToColumn(MAI->getCommentColumn());
1589 O << MAI->getCommentString() << " implicit-def: "
Chris Lattnerf806c232009-09-13 19:48:37 +00001590 << TRI->getName(MI->getOperand(0).getReg());
Evan Chengda47e6e2008-03-15 00:03:38 +00001591}
1592
Jakob Stoklund Olesenad682642009-11-04 19:24:37 +00001593void AsmPrinter::printKill(const MachineInstr *MI) const {
1594 if (!VerboseAsm) return;
1595 O.PadToColumn(MAI->getCommentColumn());
1596 O << MAI->getCommentString() << " kill:";
1597 for (unsigned n = 0, e = MI->getNumOperands(); n != e; ++n) {
1598 const MachineOperand &op = MI->getOperand(n);
1599 assert(op.isReg() && "KILL instruction must have only register operands");
1600 O << ' ' << TRI->getName(op.getReg()) << (op.isDef() ? "<def>" : "<kill>");
1601 }
1602}
1603
Jim Laskey1ee29252007-01-26 14:34:52 +00001604/// printLabel - This method prints a local label used by debug and
1605/// exception handling tables.
1606void AsmPrinter::printLabel(const MachineInstr *MI) const {
Dan Gohman528bc022008-07-01 00:16:26 +00001607 printLabel(MI->getOperand(0).getImm());
Jim Laskey1ee29252007-01-26 14:34:52 +00001608}
1609
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001610void AsmPrinter::printLabel(unsigned Id) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001611 O << MAI->getPrivateGlobalPrefix() << "label" << Id << ':';
Evan Cheng1b08bbc2008-02-01 09:10:45 +00001612}
1613
Chris Lattner66099132006-02-01 22:41:11 +00001614/// PrintAsmOperand - Print the specified operand of MI, an INLINEASM
1615/// instruction, using the specified assembler variant. Targets should
1616/// overried this to format as appropriate.
1617bool AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Chris Lattnera36cb0a2006-02-06 22:17:23 +00001618 unsigned AsmVariant, const char *ExtraCode) {
Chris Lattner66099132006-02-01 22:41:11 +00001619 // Target doesn't support this yet!
1620 return true;
Chris Lattner0264d1a2006-01-27 02:10:10 +00001621}
Chris Lattnerdd260332006-02-24 20:21:58 +00001622
1623bool AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNo,
1624 unsigned AsmVariant,
1625 const char *ExtraCode) {
1626 // Target doesn't support this yet!
1627 return true;
1628}
Nate Begeman37efe672006-04-22 18:53:45 +00001629
Dan Gohman8c2b5252009-10-30 01:27:03 +00001630MCSymbol *AsmPrinter::GetBlockAddressSymbol(const BlockAddress *BA) const {
1631 return GetBlockAddressSymbol(BA->getFunction(), BA->getBasicBlock());
1632}
1633
1634MCSymbol *AsmPrinter::GetBlockAddressSymbol(const Function *F,
1635 const BasicBlock *BB) const {
1636 assert(BB->hasName() &&
1637 "Address of anonymous basic block not supported yet!");
1638
Dan Gohman6ae5b322009-10-30 01:38:20 +00001639 // FIXME: This isn't guaranteed to produce a unique name even if the
1640 // block and function have a name.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001641 std::string Mangled =
1642 Mang->getMangledName(F, Mang->makeNameProper(BB->getName()).c_str(),
1643 /*ForcePrivate=*/true);
1644
1645 return OutContext.GetOrCreateSymbol(StringRef(Mangled));
1646}
1647
Chris Lattner7cb384d2009-09-12 23:02:08 +00001648MCSymbol *AsmPrinter::GetMBBSymbol(unsigned MBBID) const {
1649 SmallString<60> Name;
1650 raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BB"
1651 << getFunctionNumber() << '_' << MBBID;
1652
1653 return OutContext.GetOrCreateSymbol(Name.str());
1654}
1655
1656
Chris Lattner70a54c02009-09-13 18:25:37 +00001657/// EmitBasicBlockStart - This method prints the label for the specified
1658/// MachineBasicBlock, an alignment (if present) and a comment describing
1659/// it if appropriate.
Chris Lattner662316c2009-09-14 03:15:54 +00001660void AsmPrinter::EmitBasicBlockStart(const MachineBasicBlock *MBB) const {
Dan Gohmanb1cac332009-10-30 01:34:35 +00001661 // Emit an alignment directive for this block, if needed.
Chris Lattner70a54c02009-09-13 18:25:37 +00001662 if (unsigned Align = MBB->getAlignment())
1663 EmitAlignment(Log2_32(Align));
Evan Chengfb8075d2008-02-28 00:43:03 +00001664
Dan Gohmanb1cac332009-10-30 01:34:35 +00001665 // If the block has its address taken, emit a special label to satisfy
1666 // references to the block. This is done so that we don't need to
1667 // remember the number of this label, and so that we can make
1668 // forward references to labels without knowing what their numbers
1669 // will be.
Dan Gohman8c2b5252009-10-30 01:27:03 +00001670 if (MBB->hasAddressTaken()) {
1671 GetBlockAddressSymbol(MBB->getBasicBlock()->getParent(),
1672 MBB->getBasicBlock())->print(O, MAI);
1673 O << ':';
1674 if (VerboseAsm) {
1675 O.PadToColumn(MAI->getCommentColumn());
1676 O << MAI->getCommentString() << " Address Taken";
1677 }
1678 O << '\n';
1679 }
1680
Dan Gohmanb1cac332009-10-30 01:34:35 +00001681 // Print the main label for the block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001682 if (MBB->pred_empty() || MBB->isOnlyReachableByFallthrough()) {
1683 if (VerboseAsm)
1684 O << MAI->getCommentString() << " BB#" << MBB->getNumber() << ':';
1685 } else {
1686 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
1687 O << ':';
1688 if (!VerboseAsm)
1689 O << '\n';
1690 }
Chris Lattner70a54c02009-09-13 18:25:37 +00001691
Dan Gohmanb1cac332009-10-30 01:34:35 +00001692 // Print some comments to accompany the label.
Chris Lattner70a54c02009-09-13 18:25:37 +00001693 if (VerboseAsm) {
Dan Gohmane45da0d2009-08-12 18:47:05 +00001694 if (const BasicBlock *BB = MBB->getBasicBlock())
1695 if (BB->hasName()) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001696 O.PadToColumn(MAI->getCommentColumn());
1697 O << MAI->getCommentString() << ' ';
Dan Gohman46d50562009-08-12 20:56:56 +00001698 WriteAsOperand(O, BB, /*PrintType=*/false);
Dan Gohmane45da0d2009-08-12 18:47:05 +00001699 }
David Greeneb71d1b22009-08-10 16:38:07 +00001700
Chris Lattner70a54c02009-09-13 18:25:37 +00001701 EmitComments(*MBB);
Dan Gohmane3cc3f32009-10-06 17:38:38 +00001702 O << '\n';
David Greeneb71d1b22009-08-10 16:38:07 +00001703 }
Nate Begeman37efe672006-04-22 18:53:45 +00001704}
Nate Begeman52a51e382006-08-12 21:29:52 +00001705
Evan Chengcc415862007-11-09 01:32:10 +00001706/// printPICJumpTableSetLabel - This method prints a set label for the
1707/// specified MachineBasicBlock for a jumptable entry.
1708void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
1709 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001710 if (!MAI->getSetDirective())
Nate Begeman52a51e382006-08-12 21:29:52 +00001711 return;
1712
Chris Lattner33adcfb2009-08-22 21:43:10 +00001713 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001714 << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001715 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001716 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001717 << '_' << uid << '\n';
Nate Begeman52a51e382006-08-12 21:29:52 +00001718}
Evan Chengd6594ae2006-09-12 21:00:35 +00001719
Evan Chengcc415862007-11-09 01:32:10 +00001720void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
1721 const MachineBasicBlock *MBB) const {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001722 if (!MAI->getSetDirective())
Evan Cheng41349c12006-11-01 09:23:08 +00001723 return;
1724
Chris Lattner33adcfb2009-08-22 21:43:10 +00001725 O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
Evan Cheng347d39f2007-10-14 05:57:21 +00001726 << getFunctionNumber() << '_' << uid << '_' << uid2
1727 << "_set_" << MBB->getNumber() << ',';
Chris Lattner325d3dc2009-09-13 17:14:04 +00001728 GetMBBSymbol(MBB->getNumber())->print(O, MAI);
Chris Lattner33adcfb2009-08-22 21:43:10 +00001729 O << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
Evan Cheng347d39f2007-10-14 05:57:21 +00001730 << '_' << uid << '_' << uid2 << '\n';
Evan Cheng41349c12006-11-01 09:23:08 +00001731}
1732
Evan Chengd6594ae2006-09-12 21:00:35 +00001733/// printDataDirective - This method prints the asm directive for the
1734/// specified type.
Sanjiv Guptac8d7bc82009-01-30 04:25:10 +00001735void AsmPrinter::printDataDirective(const Type *type, unsigned AddrSpace) {
Evan Chengd6594ae2006-09-12 21:00:35 +00001736 const TargetData *TD = TM.getTargetData();
1737 switch (type->getTypeID()) {
Chris Lattnerf1cfea22009-07-15 04:42:49 +00001738 case Type::FloatTyID: case Type::DoubleTyID:
1739 case Type::X86_FP80TyID: case Type::FP128TyID: case Type::PPC_FP128TyID:
1740 assert(0 && "Should have already output floating point constant.");
1741 default:
1742 assert(0 && "Can't handle printing this type of thing");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001743 case Type::IntegerTyID: {
1744 unsigned BitWidth = cast<IntegerType>(type)->getBitWidth();
1745 if (BitWidth <= 8)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001746 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001747 else if (BitWidth <= 16)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001748 O << MAI->getData16bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001749 else if (BitWidth <= 32)
Chris Lattner33adcfb2009-08-22 21:43:10 +00001750 O << MAI->getData32bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001751 else if (BitWidth <= 64) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001752 assert(MAI->getData64bitsDirective(AddrSpace) &&
Reid Spencera54b7cb2007-01-12 07:05:14 +00001753 "Target cannot handle 64-bit constant exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001754 O << MAI->getData64bitsDirective(AddrSpace);
Dan Gohman82f94f12008-09-08 16:40:13 +00001755 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +00001756 llvm_unreachable("Target cannot handle given data directive width!");
Reid Spencera54b7cb2007-01-12 07:05:14 +00001757 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001758 break;
Reid Spencera54b7cb2007-01-12 07:05:14 +00001759 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001760 case Type::PointerTyID:
1761 if (TD->getPointerSize() == 8) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001762 assert(MAI->getData64bitsDirective(AddrSpace) &&
Evan Chengd6594ae2006-09-12 21:00:35 +00001763 "Target cannot handle 64-bit pointer exprs!");
Chris Lattner33adcfb2009-08-22 21:43:10 +00001764 O << MAI->getData64bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001765 } else if (TD->getPointerSize() == 2) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001766 O << MAI->getData16bitsDirective(AddrSpace);
Sanjiv Guptafcc6f152009-01-22 10:14:21 +00001767 } else if (TD->getPointerSize() == 1) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001768 O << MAI->getData8bitsDirective(AddrSpace);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001769 } else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001770 O << MAI->getData32bitsDirective(AddrSpace);
Evan Chengd6594ae2006-09-12 21:00:35 +00001771 }
Evan Chengd6594ae2006-09-12 21:00:35 +00001772 break;
Evan Chengd6594ae2006-09-12 21:00:35 +00001773 }
1774}
Dale Johannesen4c3a5f82007-02-16 01:54:53 +00001775
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001776void AsmPrinter::printVisibility(const std::string& Name,
1777 unsigned Visibility) const {
1778 if (Visibility == GlobalValue::HiddenVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001779 if (const char *Directive = MAI->getHiddenDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001780 O << Directive << Name << '\n';
1781 } else if (Visibility == GlobalValue::ProtectedVisibility) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001782 if (const char *Directive = MAI->getProtectedDirective())
Anton Korobeynikovf5b6a472008-08-08 18:25:07 +00001783 O << Directive << Name << '\n';
1784 }
1785}
Gordon Henriksenc317a602008-08-17 12:08:44 +00001786
Anton Korobeynikov7751ad92008-11-22 16:15:34 +00001787void AsmPrinter::printOffset(int64_t Offset) const {
1788 if (Offset > 0)
1789 O << '+' << Offset;
1790 else if (Offset < 0)
1791 O << Offset;
1792}
1793
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001794GCMetadataPrinter *AsmPrinter::GetOrCreateGCPrinter(GCStrategy *S) {
1795 if (!S->usesMetadata())
Gordon Henriksenc317a602008-08-17 12:08:44 +00001796 return 0;
1797
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001798 gcp_iterator GCPI = GCMetadataPrinters.find(S);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001799 if (GCPI != GCMetadataPrinters.end())
1800 return GCPI->second;
1801
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001802 const char *Name = S->getName().c_str();
Gordon Henriksenc317a602008-08-17 12:08:44 +00001803
1804 for (GCMetadataPrinterRegistry::iterator
1805 I = GCMetadataPrinterRegistry::begin(),
1806 E = GCMetadataPrinterRegistry::end(); I != E; ++I)
1807 if (strcmp(Name, I->getName()) == 0) {
Gordon Henriksen5eca0752008-08-17 18:44:35 +00001808 GCMetadataPrinter *GMP = I->instantiate();
1809 GMP->S = S;
1810 GCMetadataPrinters.insert(std::make_pair(S, GMP));
1811 return GMP;
Gordon Henriksenc317a602008-08-17 12:08:44 +00001812 }
1813
Chris Lattner103289e2009-08-23 07:19:13 +00001814 errs() << "no GCMetadataPrinter registered for GC: " << Name << "\n";
Torok Edwinc23197a2009-07-14 16:55:14 +00001815 llvm_unreachable(0);
Gordon Henriksenc317a602008-08-17 12:08:44 +00001816}
David Greene014700c2009-07-13 20:25:48 +00001817
1818/// EmitComments - Pretty-print comments for instructions
Chris Lattner5f51cd02009-08-07 23:42:01 +00001819void AsmPrinter::EmitComments(const MachineInstr &MI) const {
Chris Lattnerc5ea2632009-09-09 23:14:36 +00001820 assert(VerboseAsm && !MI.getDebugLoc().isUnknown());
Chris Lattner5f51cd02009-08-07 23:42:01 +00001821
1822 DebugLocTuple DLT = MF->getDebugLocTuple(MI.getDebugLoc());
David Greene3ac1ab82009-07-16 22:24:20 +00001823
Chris Lattner8f4b1ec2009-08-17 15:48:08 +00001824 // Print source line info.
Chris Lattner33adcfb2009-08-22 21:43:10 +00001825 O.PadToColumn(MAI->getCommentColumn());
1826 O << MAI->getCommentString() << " SrcLine ";
Devang Patel1619dc32009-10-13 23:28:53 +00001827 if (DLT.Scope) {
1828 DICompileUnit CU(DLT.Scope);
1829 if (!CU.isNull())
1830 O << CU.getFilename() << " ";
David Greene3ac1ab82009-07-16 22:24:20 +00001831 }
Chris Lattner5f51cd02009-08-07 23:42:01 +00001832 O << DLT.Line;
1833 if (DLT.Col != 0)
1834 O << ":" << DLT.Col;
David Greene014700c2009-07-13 20:25:48 +00001835}
1836
David Greenefe37ab32009-08-18 19:22:55 +00001837/// PrintChildLoopComment - Print comments about child loops within
1838/// the loop for this basic block, with nesting.
1839///
1840static void PrintChildLoopComment(formatted_raw_ostream &O,
1841 const MachineLoop *loop,
Chris Lattner33adcfb2009-08-22 21:43:10 +00001842 const MCAsmInfo *MAI,
David Greenefe37ab32009-08-18 19:22:55 +00001843 int FunctionNumber) {
1844 // Add child loop information
1845 for(MachineLoop::iterator cl = loop->begin(),
1846 clend = loop->end();
1847 cl != clend;
1848 ++cl) {
1849 MachineBasicBlock *Header = (*cl)->getHeader();
1850 assert(Header && "No header for loop");
1851
1852 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001853 O.PadToColumn(MAI->getCommentColumn());
David Greenefe37ab32009-08-18 19:22:55 +00001854
Chris Lattner33adcfb2009-08-22 21:43:10 +00001855 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001856 O.indent(((*cl)->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001857 << " Child Loop BB" << FunctionNumber << "_"
1858 << Header->getNumber() << " Depth " << (*cl)->getLoopDepth();
1859
Chris Lattner33adcfb2009-08-22 21:43:10 +00001860 PrintChildLoopComment(O, *cl, MAI, FunctionNumber);
David Greenefe37ab32009-08-18 19:22:55 +00001861 }
1862}
1863
David Greeneb71d1b22009-08-10 16:38:07 +00001864/// EmitComments - Pretty-print comments for basic blocks
1865void AsmPrinter::EmitComments(const MachineBasicBlock &MBB) const
1866{
David Greenefe37ab32009-08-18 19:22:55 +00001867 if (VerboseAsm) {
David Greeneb71d1b22009-08-10 16:38:07 +00001868 // Add loop depth information
1869 const MachineLoop *loop = LI->getLoopFor(&MBB);
1870
1871 if (loop) {
1872 // Print a newline after bb# annotation.
1873 O << "\n";
Chris Lattner33adcfb2009-08-22 21:43:10 +00001874 O.PadToColumn(MAI->getCommentColumn());
1875 O << MAI->getCommentString() << " Loop Depth " << loop->getLoopDepth()
David Greeneb71d1b22009-08-10 16:38:07 +00001876 << '\n';
1877
Chris Lattner33adcfb2009-08-22 21:43:10 +00001878 O.PadToColumn(MAI->getCommentColumn());
David Greeneb71d1b22009-08-10 16:38:07 +00001879
1880 MachineBasicBlock *Header = loop->getHeader();
1881 assert(Header && "No header for loop");
1882
1883 if (Header == &MBB) {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001884 O << MAI->getCommentString() << " Loop Header";
1885 PrintChildLoopComment(O, loop, MAI, getFunctionNumber());
David Greeneb71d1b22009-08-10 16:38:07 +00001886 }
1887 else {
Chris Lattner33adcfb2009-08-22 21:43:10 +00001888 O << MAI->getCommentString() << " Loop Header is BB"
David Greeneb71d1b22009-08-10 16:38:07 +00001889 << getFunctionNumber() << "_" << loop->getHeader()->getNumber();
1890 }
1891
1892 if (loop->empty()) {
1893 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001894 O.PadToColumn(MAI->getCommentColumn());
1895 O << MAI->getCommentString() << " Inner Loop";
David Greeneb71d1b22009-08-10 16:38:07 +00001896 }
1897
1898 // Add parent loop information
1899 for (const MachineLoop *CurLoop = loop->getParentLoop();
1900 CurLoop;
1901 CurLoop = CurLoop->getParentLoop()) {
1902 MachineBasicBlock *Header = CurLoop->getHeader();
1903 assert(Header && "No header for loop");
1904
1905 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +00001906 O.PadToColumn(MAI->getCommentColumn());
1907 O << MAI->getCommentString();
Chris Lattnerc281de12009-08-23 00:51:00 +00001908 O.indent((CurLoop->getLoopDepth()-1)*2)
David Greenefe37ab32009-08-18 19:22:55 +00001909 << " Inside Loop BB" << getFunctionNumber() << "_"
David Greeneb71d1b22009-08-10 16:38:07 +00001910 << Header->getNumber() << " Depth " << CurLoop->getLoopDepth();
1911 }
1912 }
1913 }
1914}